在滚动Android Studio后动态更新时,ListView不会保留状态

时间:2015-11-08 05:07:08

标签: android listview android-studio android-listview

我在popupWindow中创建一个ListView。目前,我的列表视图显示,部件将在现在更新,但如果单击索引更改背景颜色,滚动将其放在页面上,然后滚动回该索引,它不会保留我的更改(这是一个文本更改)。另外,截至目前,另一个问题是“parent.getChildAt(position).setBackgroundColor(Color.BLUE);”如果我点击特定索引会导致错误。

以下是包含弹出窗口和列表视图的代码部分

public void bringUpEventsPopover() {
        final LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //inflate the custom popup layout
        final View inflatedView = layoutInflater.inflate(R.layout.popup_layout,null,false);

        //find the listeview in the popup layout (we will need to construct this still)
        final ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);

        //get device size
        /*Display display = getActivity().getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);*/
        int width = view.getWidth();
        int height = view.getHeight();
        //fill the data to the list items
        setSimpleList(listView);

        //set the height depends on the device size
        //popWindow= new PopupWindow(inflatedView, size.x, size.y, true);
        popWindow= new PopupWindow(inflatedView, width, height-50, true);
        //make it focusable to show the keyboard to enter in 'edittext'
        popWindow.setFocusable(true);
        popWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //show the popup at bottom of the screen and set some margin at bottom
        popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

        //make it outside touchable to dismiss the popup window when touched outside the popup area
        popWindow.setOutsideTouchable(true);
        popWindow.update();
        final FrameLayout mainFrame = (FrameLayout) getActivity().findViewById(R.id.frametop);
        mainFrame.getForeground().setAlpha(160);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mainFrame.getForeground().setAlpha(0);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id)
            {
                // selected item
                //Event item = (Event)listView.getItemAtPosition(position);
                TextView tv = (TextView)view.findViewById(R.id.eventCreator);
                parent.getChildAt(position).setBackgroundColor(Color.BLUE);
            }
        });
    }
    void setSimpleList(ListView listView){

        eventList = new ArrayList<>();


        for (int index = 0; index < 6; index++) {
            eventList.add(new Event("BT","We gon play sports and stuff","Title","Time is "+index+"pm today","3 Hours","15/30"));
        }

        for(Event item: eventList)
        {
            System.out.println(item.getTitle()+"\n"+item.getTime()+"\n"+item.getNumPeople()+"\n");
        }
        adapter = new ItemAdapter(getActivity(),R.layout.view_event,eventList);
        listView.setAdapter(adapter);
    }

这是ListView的ItemAdapter类

public class ItemAdapter extends ArrayAdapter<Event> {

    // declaring our ArrayList of items
    private ArrayList<Event> objects;

    /* here we must override the constructor for ArrayAdapter
    * the only variable we care about now is ArrayList<Item> objects,
    * because it is the list of objects we want to display.
    */
    public ItemAdapter(Context context, int textViewResourceId, ArrayList<Event> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
    }

    /*
     * we are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // assign the view we are converting to a local variable
        View v = convertView;
        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            v = inflater.inflate(R.layout.view_event, null);
            v.setTag(position);
        }
        else
        {
            v.getTag();
        }

        /*
         * Recall that the variable position is sent in as an argument to this method.
         * The variable simply refers to the position of the current object in the list. (The ArrayAdapter
         * iterates through the list we sent it)
         *
         * Therefore, i refers to the current Item object.
         */
        Event i = objects.get(position);

        if (i != null) {

            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.

            TextView creator = (TextView) v.findViewById(R.id.eventCreator);
            TextView title = (TextView) v.findViewById(R.id.eventTitle);
            TextView description = (TextView) v.findViewById(R.id.eventDescription);
            TextView time = (TextView) v.findViewById(R.id.eventTime);
            TextView duration = (TextView) v.findViewById(R.id.eventDuration);
            TextView numPeople = (TextView) v.findViewById(R.id.eventAttendance);

            // check to see if each individual textview is null.
            // if not, assign some text!
            if(creator!=null)
            {
                creator.setText("Creator: "+i.getCreator());
            }
            if (title != null){
                title.setText("Title: "+i.getTitle());
            }
            if(description!=null)
            {
                description.setText("Description: "+i.getDescription());
            }
            if( time != null) {
                time.setText("Time: "+i.getTime());
            }
            if(duration!=null)
            {
                duration.setText("Duration: "+i.getDuration());
            }
            if (numPeople != null){
                numPeople.setText(i.getNumPeople());
            }
        }

        // the view must be returned to our activity
        return v;

    }


}

这是每个列表项的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/eventCreator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="false"
        android:text="Creator: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_margin="5dp" />
    <TextView
        android:id="@+id/eventTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventCreator"
        android:text="Title: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/eventDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventTitle"
        android:text="Description: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/eventTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventDescription"
        android:text="Time: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/eventDuration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventTime"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Duration: "
        android:layout_margin="5dp" />

    <TextView
        android:id="@+id/eventAttendance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventDuration"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="15/30 Going"
        android:layout_margin="5dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eventAttendance"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_margin="20dp"
        android:text="I'm Going!"
        android:layout_centerHorizontal="true"
        android:focusable="false"
        android:clickable="false"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

两个想法。 1)也许考虑一个容器为您的事件/项目,以便您可以给他们动态ID,然后使用它们来设置背景颜色。当它滚动/关闭屏幕时,你可能会更幸运地保存状态。 2)我对此并不是百分之百确定,所以不要把它当作福音,但我认为当你将视图中的列表视图滚动时,适配器可能会混淆子结构中的索引变量。有没有其他方法可以访问该列表视图的子节点?对不起,我对这个问题没有一个好的答案,只是一个让你追逐的想法。祝你好运!

  • 乔恩

答案 1 :(得分:0)

我自己想通了。因此阵列适配器在设置时有几个部分。

// first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (v == null) {
            /* This is where you initialize new rows, by:
         *  - Inflating the layout,
         *  - Instantiating the ViewHolder,
         *  - And defining any characteristics that are consistent for every row */
            v = inflater.inflate(R.layout.view_event, null);
            v.setTag(position);
        }
        else
        {
            /* Fetch data already in the row layout,
         *    primarily you only use this to get a copy of the ViewHolder */
            v.getTag();
        }
     /* Set the data that changes in each row, like `title` and `size`
     *    This is where you give rows there unique values. */

所以我在我的事件类中设置了一个新变量来保存&#34; isSelected&#34;字段,我的&#34; onItemClickListener&#34;设置,然后通知ArrayAdapter数据更改。 在数组适配器中,我只是检查了Event.isSelected()var并相应地加载了背景。

改变背景的myArrayAdapter:

        if(objects.get(position).isSelected())
        {
            v.setBackgroundColor(Color.CYAN);
        }
        else
        {
            v.setBackgroundColor(Color.WHITE);
        }

myItemClickListener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id)
            {

                // selected item
                adapter.objects.get(position).setSelected(true);
                adapter.notifyDataSetChanged();
            }
        });