所选项目不适用于listview

时间:2015-05-18 13:18:25

标签: java android listview

我的HomeLayout中有listview(android.support.v7.widget.RecyclerView)。

HomeLayout中的代码

<LinearLayout
        p1:orientation="horizontal"
        p1:minWidth="25px"
        p1:clickable="true"
        p1:minHeight="25px"
        p1:layout_width="wrap_content"
        p1:layout_height="45dp"
        p1:id="@+id/linearLayout2"
        p1:layout_alignParentTop="true"
        p1:layout_alignParentLeft="true"
        p1:layout_marginLeft="70dp"
        p1:layout_marginTop="15dp"
        p1:layout_marginRight="310dp">
        <android.support.v7.widget.RecyclerView
            p1:id="@+id/categoriesListView"
            p1:duplicateParentState="true"
            p1:state_checked="true"
            p1:focusable="true"
            p1:focusableInTouchMode="true"
            p1:clickable="true"
            p1:scrollbars="horizontal"
            p1:state_activated="true"
            p1:layout_width="match_parent"
            p1:layout_height="45dp" />
    </LinearLayout>

列表视图具有自定义适配器。 适配器代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="horizontal"
    p1:focusable="true"
    p1:focusableInTouchMode="true"
    p1:clickable="true"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/linearLayout1">
    <TextView
        p1:text="Medium Text"
        p1:background="@drawable/designforselected"
        p1:textAppearance="?android:attr/textAppearanceMedium"
        p1:layout_width="wrap_content"
        p1:layout_height="45dp"
        p1:id="@+id/txtCategoryName"
        p1:textSize="20dp"
        p1:focusable="true"
        p1:focusableInTouchMode="true"
        p1:clickable="true"
        p1:textColor="@drawable/textcolorarticle"
        p1:gravity="center"
        p1:fontFamily="Myriad"
        p1:paddingRight="20dp"
        p1:paddingLeft="20dp"
        p1:paddingTop="10dp"
        p1:paddingBottom="10dp"
        p1:layout_marginLeft="5dp" />
</LinearLayout>

Textview有自定义设计按p1:background="@drawable/designforselected" 设计代码

    <?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
  <shape android:shape="rectangle"/>
    <solid android:color="#333333" />
</item>
  <item android:state_pressed="true" >
  <shape android:shape="rectangle"/>
    <solid android:color="#333333" />
  </item>  

<item>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#b3b3b3"
        android:endColor="#e6e6e6"
        android:angle="90"/>  
        <corners
        android:topLeftRadius="5dp" android:topRightRadius="5dp"   android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"   >
    </corners>  
</shape>
</item>
</selector>

State_pressed是工作但state_selected没有工作。为什么?泰 我希望解决XML文件

3 个答案:

答案 0 :(得分:0)

您需要像这样实施listView onClick

listContent.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            String item = ...;

            Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

        }
    });

答案 1 :(得分:0)

我不久前和state_selected有过类似的问题。请改为state_checkedstate_activated

答案 2 :(得分:0)

在您的适配器中写下:

@Override
    public void onBindViewHolder(ViewHolder viewHolder, final int i) {
        final GridItem nature = mItems.get(i);
        viewHolder.tvspecies.setText(nature.getName());
        viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());

        viewHolder.imgThumbnail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("CLick",nature.toString());
            }
        });
    }

这是一种对我有用的方式。当然你需要使用你的ids