android中的ListView并没有突出显示我选择的行

时间:2017-08-07 08:36:27

标签: android listview android-custom-view custom-adapter

当我在ListView中点击一行时ListView没有显示任何效果我想在iOS中选择或点击一行时显示某些效果或任何颜色等我使用自定义视图和自定义适配器对于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 
   }

,这是XML中的ListView

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

有人请吗?

2 个答案:

答案 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>

答案 1 :(得分: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); 
        }
    });