2015-11-28 3 views
-1

Я использую listview я следующее xml с формой, чтобы установить в listitemна ListItem клик цвет должен оставаться таким же

one.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#D5F4FF"/> 
    <stroke android:width="1dp" android:color="#979797"/> 
    <corners 
     android:bottomRightRadius="3dp" 
     android:bottomLeftRadius="3dp" 
     android:topLeftRadius="3dp" 
     android:topRightRadius="3dp"/> 
    <padding android:left="5dp" android:top="5dp" 
     android:right="5dp" android:bottom="5dp" /> 
</shape> 

two.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FFA500"/> 
    <stroke android:width="1dp" android:color="#979797"/> 
    <corners 
     android:bottomRightRadius="3dp" 
     android:bottomLeftRadius="3dp" 
     android:topLeftRadius="3dp" 
     android:topRightRadius="3dp"/> 
    <padding android:left="5dp" android:top="5dp" 
     android:right="5dp" android:bottom="5dp" /> 
</shape> 

Теперь, по умолчанию, я хочу установить one.xml к моему ListItem и когда пользователь нажимает на listitem он должен установить two.xml и она по-прежнему остается такой же, но ее не работает я уже пытался с listselector свойством listview.

CustomAdapter

private class CustomAdapterGiftsharealert extends BaseAdapter { 
     // String [] result; 
     Context context; 
     // int [] imageId; 
     private ArrayList<String> listData; 
     private LayoutInflater inflater=null; 
     public CustomAdapterGiftsharealert(Context mainActivity, ArrayList<String> listData) { 
      // TODO Auto-generated constructor stub 
      context=mainActivity; 
      this.listData=listData; 

      inflater = (LayoutInflater)context. 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 
     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return listData.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     public class Holder 
     { 
      TextView tv; 

      public TextView tvbday; 

      public TextView tvrelation; 
      public LinearLayout linr; 
     } 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      final Holder holder=new Holder(); 
      View rowView; 
      rowView = inflater.inflate(R.layout.list_item_frndsfmly, null); 

      holder.linr=(LinearLayout)rowView.findViewById(R.id.linear_listitem_frndsmly); 
      holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name); 
      holder.tvbday=(TextView) rowView.findViewById(R.id.frndsfamly_bady); 
      holder.tvrelation=(TextView) rowView.findViewById(R.id.frndsfamly_realtion); 
      holder.tvbday=(TextView) rowView.findViewById(R.id.frndsfamly_bady); 
      holder.tv.setText(listData.get(position)); 
      holder.tvbday.setText(bdatedata.get(position)); 


      // holder.tvrelation.setText(testlist.get(position)); 



      return rowView; 
     } 

    } 
+0

Покажите свой соответствующий Java пожалуйста –

+0

все еще остаются такими же означает, что ??? – Pankaj

+0

@MrsEd в java i dint установить любой фон – albert

ответ

0

Использование ниже selector xml назовите view_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"><shape android:shape="rectangle"> 
      <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" /> 

      <solid android:color="@color/view_pressed" /> 
     </shape></item> 

    <item android:state_focused="true"><shape android:shape="rectangle"> 
      <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" /> 

      <solid android:color="@color/view_pressed" /> 
     </shape></item> 

    <item><shape android:shape="rectangle"> 
      <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" /> 

      <solid android:color="@color/view_normal" /> 
     </shape></item> 
</selector> 

И добавить цвета в colors.xml:

<color name="view_pressed">#FFA500</color> 
<color name="view_normal">#D5F4FF</color> 

Предположим, что для примера у вас есть пользовательский адаптер, который имеет макет, который вы можете установить как background в корневом теге. В качестве примера:

custom_adapter_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:background="@drawable/view_selector" > 

    <TextView 
     style="@style/textviewstyle_Medium" 
     android:id="@+id/textview1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:lines="2" 
     android:text="@string/textview1"/> 

     <TextView 
     style="@style/textviewstyle_small" 
     android:id="@+id/textview2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left|center" 
     android:padding="2dp" 
     android:text="@string/textview2"/> 

</LinearLayout> 
+0

но где и как установить ?? – albert

+0

У меня есть пользовательский адаптер – albert

+0

Можете ли вы рассказать? – albert

0

Для выбора списка для работы необходимо установить режим ListeView выбор:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

и переопределить

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // Make the newly clicked item the currently selected one. 
    getListView().setItemChecked(position, true); 
} 

Примечание: Убедитесь, что макет для пунктов поддерживает выбор, или использовать андроид файл макета: android.R.layout.simple_list_item_activated_1