【问题标题】:ListView in android is not highlighting the row i am selectingandroid中的ListView没有突出显示我选择的行
【发布时间】:2017-08-07 08:36:27
【问题描述】:

当我在 ListView 中点击一行时,ListView 没有显示任何效果我想在 iOS 中显示一些效果或任何颜色等,当我们选择或点击一行时,我正在使用自定义视图和自定义适配器对于 ListView 和 infalting 自定义视图

我在这里创建一个带有自定义视图的自定义适配器

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 />

有人吗??

【问题讨论】:

  • 你想在选中时突出显示项目视图还是只选择选择器项目?

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


【解决方案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>

【讨论】:

    【解决方案2】:

    只需使用

    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); 
            }
        });
    

    【讨论】:

    • 如何突出显示选定的行?
    • 我已经编辑了我的答案并添加了这行代码 arg1.setBackgroundColor(Color.GREEN);
    • 当另一个项目被选中时会发生什么?它将如何获得原来的颜色?当通过键盘上的按键选择项目时,这应该如何工作?
    • 它只是突出显示另一个项目,如果你想在最后有一个选定项目的列表,我认为最好有一个 customAdatper (这就是我所做的)并有一个复选框知道选择或取消选择什么或更好,您可以尝试 SparseBooleanArray (如果您不需要更改项目视图选择这个,它非常快),并且在取消选择时您可以将其设置为原始颜色,对于物理键通使用 listview.setOnKeyListener
    • 如果您在选择@Ricardo 的以下答案时只需要更改颜色,在这种情况下它比我的更干净
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多