2017-08-07 42 views
0

当我在ListView中点击一行时,ListView没有显示任何效果我想在iOS中选择或点击某行时显示一些效果或任何颜色等。我使用的自定义视图和ListView的自定义适配器和infaltingAndroid中的ListView没有突出显示我选择的行

我在这里创建自定义视图自定义适配器的自定义视图

CustomContactsHomeAdapter customContactsAdapter = new CustomContactsHomeAdapter(HomeView.this, R.layout.custom_contact_cell, userBean_home_Search.getData(), 
listView.setAdapter(customContactsAdapter); 

这是我的自定义适配器

public class CustomContactsHomeAdapter extends ArrayAdapter<UserBean_Home.DataBean> { 

    public UserBean_Home userBean; 
    Context mContext; 

    public CustomContactsHomeAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    public CustomContactsHomeAdapter(Context context, int resource, List<UserBean_Home.DataBean> need, UserBean_Home abc) { 
     super(context, resource , need); 
     this.mContext = context; 
     this.userBean = abc; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     if(convertView==null) { 
      LayoutInflater vi; 
      vi = LayoutInflater.from(mContext); 
      convertView = vi.inflate(R.layout.custom_contact_cell, null); 

      TextView contactName = (TextView) convertView.findViewById(R.id.contactName); 
      TextView contactDesc = (TextView) convertView.findViewById(R.id.contactAddress); 
      ImageView profileImage = (ImageView) convertView.findViewById(R.id.profileImage); 

      UserBean_Home.DataBean dataBean = userBean.getData().get(position); 

      if(dataBean.getR_type().equalsIgnoreCase("1")) { 
       contactName.setText(dataBean.getFull_name()); 
       contactDesc.setText("@"+dataBean.getUser_name()); 
       Picasso.with(mContext) 
         .load(GlobalBean.IMAGES_URL+dataBean.getImage()) 
         .placeholder(R.drawable.noimage) 
         .into(profileImage); 
      } else { 

       contactName.setText(dataBean.getGroup_title()); 
       contactDesc.setText("@"+dataBean.getGroup_description()); 
       Picasso.with(mContext) 
         .load(GlobalBean.IMAGES_URL+dataBean.getGroup_cover_image()) 
         .placeholder(R.drawable.noimage) 
         .into(profileImage); 
      } 
//   if(dataBean.getUnread_messages().equalsIgnoreCase("0") == false) { 
//    TextView undreadMessages = (TextView) convertView.findViewById(R.id.undreadMessages); 
//    undreadMessages.setVisibility(View.VISIBLE); 
//    undreadMessages.setText(dataBean.getUnread_messages()); 
//   } 
    } 
     return convertView; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return getCount(); 
    } 
    @Override 
    public int getItemViewType(int position) { 
     return 
    } 

,这是在的ListView XML

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView" 
    android:layout_below="@id/L1 /> 

请人?

+0

您想选择或只选择一项项时,突出项目的看法? –

回答

0

只使用

listview.setOnItemClickListener

设置适配器像波纹管

listview.setOnItemClickListener(new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) 
     { 
      Toast.makeText(yourActivity.this, "" + position, Toast.LENGTH_SHORT).show(); 
      arg1.setBackgroundColor(Color.GREEN); 
     } 
    }); 
+0

这是如何突出显示所选行的? –

+0

我编辑了我的答案,并添加了这行代码arg1.setBackgroundColor(Color.GREEN); –

+0

当另一个项目被选中时会发生什么?它将如何获得它的原始颜色?如何通过键盘上的按键选择某个项目时该如何工作? –

1

后,你所需要的就是要应用到ListView项的选择。这里有一个例子:

选择mySelector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/unpressed" /> 

</selector> 

项目例如:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/ftaccountrow" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/mySelector" > 

    ..... 

</RelativeLayout> 
相关问题