2015-05-18 46 views
0

我在我的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"代碼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文件

回答

0

您需要實現listViewonClick這樣的:

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

     } 
    }); 
+0

我想解決XML文件 –

0

我有類似的問題,不久前與state_selected。改爲嘗試state_checkedstate_activated

+0

沒有工作,我用xamarin.andorid –

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

+0

我想解決XML文件 –

+0

@IbrahimSušić它不是最好的,正確的方式我.. –

+0

爲什麼如果XML可以選擇更改所選項目的顏色 –

相關問題