2017-04-04 88 views
0

在我的应用程序中,有一个ListView,我从CustomAdapter中将一些数据放在其中,其中我将项目的设计更改为我使用其中的按钮创建的项目,如下所示:从CustomAdapter中更改ListView的项目位置

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/layout" 
    android:padding="15dp"> 
    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:textColor="@android:color/black" 
     android:text="Title" 
     android:textAlignment="center"/> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:textColor="@android:color/black" 
     android:text="Button" /> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_marginTop="5dp" 
     android:background="@android:color/black"/> 
</LinearLayout> 

这里是我的CustomAdapter:

class CustomAdapter extends ArrayAdapter<String> { 

    private Context context; 
    private List<String> items; 

    CustomAdapter(@NonNull Context context, @NonNull String[] items) { 
     super(context, 0, items); 

     this.context = context; 
     this.items = Arrays.asList(items); 
    } 

    @NonNull 
    @Override 
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
     Holder holder = new Holder(); 
     if(convertView == null){ 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.custom_list_item, parent, false); 

      holder.layout = (LinearLayout) convertView.findViewById(R.id.layout); 
      holder.textView = (TextView) convertView.findViewById(R.id.textView); 
      holder.button = (Button) convertView.findViewById(R.id.button); 

      convertView.setTag(holder); 
     }else { 
      holder = (CustomAdapter.Holder) convertView.getTag(); 
     } 
     final Holder finalHolder = holder; 
     final String item = items.get(position); 

     finalHolder.textView.setText(item); 
     finalHolder.button.setOnClickListener(
     new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        finalHolder.layout.setBackgroundColor(context.getColor(R.color.colorPrimary)); 

        //this is how I've tried and haven't worked 
        items.remove(position); 
        items.add(items.size()-1, item); 
        notifyDataSetChanged(); 
       } 
      } 
     ); 

     return convertView; 
    } 

    private static class Holder{ 
     LinearLayout layout; 
     TextView textView; 
     Button button; 
    } 
} 

我需要的,当你在列表中的项目之一,点击该按钮,此项目转到最后位置在列表中(其颜色改变)。

+0

不要存放收集(资料)由你自己,当你从ArrayAdapter <> – Selvin

回答

0
ListView lv = getListView(); 
setContentView(lv); 
lv.setOnItemClickListener(new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) 
    { 
     tmp = lv.get(0); 
     lv.set(0, list.get(position)); 
     lv.set(position,tmp); 
     adapter.notifyDataSetChanged(); 
    } 
}); 

您也可以通过为setSelection

ListView.setSelection(position); 
+0

获得改变列表视图的位置,这也不会做。我的需求是,当我单击项目按钮时,但是从适配器内部发生此事件。因为在我原来的程序中,我在适配器中有一些额外的测试。 – Cocholate