2015-04-01 78 views
0

我正在通過this tutorial,並且有想法使其如此,當您按下按鈕時,其中一個邊框的顏色會發生變化。更改列表項的背景邊框的顏色

我看着this solution,但沒有任何反應。

這裏是我做了什麼:

我在繪製一個XML的形狀(feed_item.xml):

<?xml version="1.0" encoding="utf-8"?> 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Bottom 2dp Shadow --> 
    <item> 
     <shape android:shape="rectangle"> 

      <solid android:color="#d8d8d8" /> 
      <corners android:radius="7dp" /> 

     </shape> 
    </item> 
    <!-- White Top color --> 
    <item android:bottom="3px" android:id="@+id/feedsquare"> 
     <shape 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle"> 
      <!-- view background color --> 
      <solid 
       android:color="@color/white" > 
      </solid> 
      <!-- view border color and width (I WANT TO CHANGE THIS) --> 
      <stroke 
       android:width="1dp" 
       android:color="@color/white" > 
      </stroke> 
      <!-- If you want to add some padding --> 
      <padding 
       android:left="4dp" 
       android:top="4dp" 
       android:right="4dp" 
       android:bottom="4dp" > 
      </padding> 
     </shape> 
    </item> 
</layer-list> 

然後,我有一個佈局,基本上是每個如何卡/名單項目看起來(list_row.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:background="@drawable/feed_item" 
    android:id="@+id/card" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:padding="2dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView android:id="@+id/title" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:layout_width="wrap_content" 
     android:text="MMMMMMMMMM" 
     android:textSize="@dimen/name3" 
     android:textColor="@color/black" /> 

    <!--android:scaleType="fitXY"--> 
    <ImageView android:id="@+id/list_image" 
     android:layout_height="180dp" 
     android:layout_margin="2dp" 
     android:layout_weight="0.04" 
     android:layout_width="match_parent" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     android:src="@mipmap/navigation_refresh"/> 

    <TextView android:id="@+id/description" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp" 
     android:layout_width="wrap_content" 
     android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" 
     android:textSize="@dimen/description3" 
     android:textColor="@color/black" /> 

</LinearLayout> 

現在,我有一個帶有listview的片段。該片段完全是教程所做的(顯示8張卡片)。我添加了一個按鈕,我想改變一個特定卡的邊框顏色(在onactivitycreated的片段)的片段:當我按下按鈕

newpost = (Button) getActivity().findViewById(R.id.reply); 
newpost.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item); 

     GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare)); 
     shape.setStroke(1 ,R.color.green); 

    } 
}); 

什麼也沒有發生。任何想法,我可以做什麼?

回答

1

要顯示在列表視圖選擇的項目(列):

  1. 創建適配器類的變量:

私人INT selectedPosition = 0;

  • 在側適配器創建方法:

    public void setSelectedItem(int position) { 
    this.selectedPosition = position; 
    notifyDataSetChanged(); 
    } 
    
  • 在getView梅索德

    如果(位置== selectedPosition) { convertView.setBackground添加檢查(context.getResources()getDrawable(R.drawable.selector_rect_black_trans_bg)。); } else convertView.setBackgroundColor(context.getResources()。getColor(android.R.color.transparent));

  • selector_rect_black_trans_bg.xml:

    <solid android:color="@color/gray1" /> 
    
    <stroke 
        android:width="2dp" 
        android:color="@color/gray6" /> 
    
    <corners 
        android:bottomLeftRadius="0dp" 
        android:bottomRightRadius="0dp" 
        android:topLeftRadius="0dp" 
        android:topRightRadius="0dp" /> 
    

  • 在活動,只需要通過適配器的位置來展示突出。 其測試代碼,它會工作。

    +0

    格式化您的代碼。它看起來太醜陋了.. – 2015-04-01 11:41:07

    相關問題