2

我在屏幕左侧有一个列表视图,单击项目时我想更新屏幕右半部分的文本,我在这里要做的是将单击的项目移动到列表视图的中心。就像如果该项目在顶部并且我单击它它会自动移动到列表视图的中心,我该怎么做?任何形式的帮助将不胜感激。

我有一个列表视图,其中 7 个项目是可见的,并且在启动时将选择第 4 个项目,因为它位于列表视图中可见项目的中心,如果有 n 个项目并且用户选择的任何项目都将位于可见项目的中心在列表视图中。就像我有 10 个项目并且在开始时选择了第 4 个项目,当用户选择第 3 个项目时,listview 中的第 n 个项目应该到达索引零并且第 3 个项目将到达位置 4。对于每个其他选定的项目也是如此?任何人都可以为此提供代码片段吗?

4

2 回答 2

1

ListView更改源中的项目顺序Array,然后调用notifyDataSetChanged()ListView Adapter

编辑:代码示例

    public class ListAdapter extends BaseAdapter{


    private Activity activity;
    private ArrayList<ListRowObject> listItems;

    public ListAdapter(Activity activity){
        this.activity = activity;
        listItems = new ArrayList<ListRowObject>();
    }

    public void addItem(ListRowObject item){
        listItems.add(item);
        notifyDataSetChanged();
    }

    public void addItems(ArrayList<ListRowObject> items){
        listItems = items;
        notifyDataSetChanged();
    }

    public void clear(){
        listItems = null;
        listItems = new ArrayList<ListRowObject>();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = activity.getLayoutInflater().inflate(R.layout.list_row, null);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.bgLayout = (LinearLayout) convertView.findViewById(R.id.bgLayout);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        ListRowObject row = listItems.get(position);

        if(row.isSelected())
            holder.bgLayout.setBackgroundColor(Color.GRAY);
        else
            holder.bgLayout.setBackgroundColor(Color.WHITE);

        holder.text.setText(row.getText());

        return convertView;
    }
}


    //--------
    public class ListRowObject {
    private String text;
    private int positionInList;
    private boolean isSelected;

    public String getText() {
        return text;
    }


    public void setText(String text) {
        this.text = text;
    }

    public int getPositionInList() {
        return positionInList;
    }

    public void setPositionInList(int positionInList) {
        this.positionInList = positionInList;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}

//------
public class Main extends Activity {

    private ListView listView;
    private ListAdapter adapter;
    private Activity activity;

    private ArrayList<ListRowObject> items;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        activity = this;

        initializeFormViews();
        initializeOnClickEvents();

        fillList();

    }

    private void initializeFormViews(){
        listView = (ListView) findViewById(R.id.listView);
    }

    private void initializeOnClickEvents(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Toast.makeText(activity, "Pressed " +position, Toast.LENGTH_SHORT).show();

                // unselect all rows
                for(ListRowObject item : items){
                    item.setSelected(false);
                }

                int first = adapterView.getFirstVisiblePosition();
                int last = adapterView.getLastVisiblePosition();

                int centerPosition = (first + last) / 2;

                // change bg for centerPosition row
                adapterView.getChildAt(centerPosition).findViewById(R.id.bgLayout).setBackgroundColor(Color.GRAY);
                changeItems(position, centerPosition);
            }
        });
    }

    private void changeItems(int pressedPosition, int centerPosition){
        ListRowObject centerRow = items.get(centerPosition);
        ListRowObject pressedRow = items.get(pressedPosition);
        pressedRow.setSelected(true);
        centerRow.setSelected(false);

        items.remove(centerPosition);
        items.add(centerPosition, pressedRow);

        items.remove(pressedPosition);
        items.add(pressedPosition, centerRow);

        adapter.clear();
        adapter.addItems(items);
    }

    private void fillList(){
        adapter = new ListAdapter(activity);
        items = new ArrayList<ListRowObject>();

        items = getItems();

        for(ListRowObject item : items){
            adapter.addItem(item);
        }

        listView.setAdapter(adapter);
    }

    private ArrayList<ListRowObject> getItems(){
        ArrayList<ListRowObject> result = new ArrayList<ListRowObject>();

        for(int i = 0; i < 15; i++){
            ListRowObject object = new ListRowObject();
            object.setPositionInList(i);
            object.setText("Item #" + i);

            if(i != 4)
                object.setSelected(false);
            else
                object.setSelected(true);


            result.add(object);
        }

        return result;
    }
}

//------
public class ViewHolder {
    public TextView text;
    public LinearLayout bgLayout;
}

list_row.xml

<?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="50dp"
                          android:id="@+id/bgLayout">

        <TextView
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:id="@+id/text"
                        android:textColor="#000000"
                        android:textSize="24dp"
                        android:gravity="center"/>
</LinearLayout>

主要的.xml

<?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="match_parent">

        <ListView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/listView"/>
</LinearLayout>
于 2012-12-26T10:41:12.367 回答
1

当您为您的列表视图创建 ArrayAdapter 时,您会向它发送一个 ListArray。当您想要更改内容时。您只需更改此 listArray,然后单击您的项目时,您可以更改 ListArray 并调用 notifyDataSetChanged(); 方法你的适配器。

于 2012-12-26T10:46:12.690 回答