2015-04-01 79 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

    相关问题