ListView项在Fragment android中自动更改

时间:2016-11-03 20:04:33

标签: android listview android-fragments

当我滚动列表视图时,项目会自动更改,换句话说,项目会覆盖其他项目的视图,例如:当我点击收藏夹按钮时其他项目已经更改为这样 例如:

the item that I clicked

I didn't click on this item

这是我的代码:

CustomListAdapter.java:

public class CustomListAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    boolean isVoter;
    public CustomListAdapter(Context c ) {
        this.context = c;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.date_view,null);

            viewHolder = new ViewHolder();

            viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);

        }else {

            viewHolder = (ViewHolder)convertView.getTag();
        }
        isVoter = newsItemArray.get(position).isVoter();

        viewHolder.vote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Utility.isNetworkAvailable(context))
                    Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
                else
                    voteBtn( position);

            }});
        return convertView;
    }


    public void voteBtn(int position){
        if ( !isVoter  ) {

            viewHolder.vote.setImageResource(R.drawable.button_pressed);
        }
        else{

            viewHolder.vote.setImageResource(R.drawable.button_normal);
        }
        isVoter = !isVoter;

    }
}

private class ViewHolder{
    ImageButton vote ;

}

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

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

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

}

date_view.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="wrap_content"
    android:orientation="vertical"
    android:background="#f2f2f2"
    android:gravity="center"
    android:elevation="8dp"
    android:layout_margin="10dp">


    <android.support.v7.widget.CardView
        android:id="@+id/newsCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff"

        android:layout_margin="10dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="10dp"
            >

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/newsimage1"
                    android:layout_width="45px"
                    android:layout_height="45px"
                    android:src="@mipmap/ic_launcher"
                    android:scaleType="centerCrop"
                    />

                <TextView
                    android:id="@+id/newsname"
                    android:layout_marginLeft="16dp"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Fox News."
                    android:textColor="#000000"
                    android:textSize="20dp"
                    android:layout_gravity="center"
                    />

                <TextView
                    android:id="@+id/time"
                    android:layout_weight="0.6"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="2 day ago"
                    android:textColor="@color/listsub1"
                    android:textSize="14dp"
                    android:layout_gravity="center"
                    />

                <ImageView
                    android:id="@+id/more"
                    android:visibility="invisible"
                    android:layout_width="15dp"
                    android:layout_height="22dp"
                    android:src="@drawable/more"
                    android:layout_gravity="center"
                    />
            </LinearLayout>


            <LinearLayout
                android:layout_marginTop="12dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:id="@+id/sourceNews"
                    android:layout_weight="1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_marginRight="5dp"
                    >

                    <TextView
                        android:id="@+id/news"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
                        android:textSize="20dp"
                        android:textColor="@color/listtext"
                        android:lineSpacingExtra="3dp"
                        />

                    <TextView
                        android:id="@+id/newssub"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
                        android:layout_marginTop="5dp"
                        android:textSize="13dp"
                        android:textColor="@color/listsub1"
                        android:lineSpacingExtra="3dp"
                        />

                    <TextView
                        android:id="@+id/votes_num"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="10dp"
                        android:textStyle="bold"
                        android:textColor="#f40000"
                        android:maxLines="1"
                        android:layout_marginTop="5dp"
                        />

                </LinearLayout>


            </LinearLayout>

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">
                <ImageButton
                    android:id="@+id/vote"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/star"
                    android:background="#00ffffff"
                    android:paddingLeft="5dp" />

                <!--
                                <ImageButton
                                    android:id="@+id/vote"
                                    android:layout_width="0dp"
                                    android:layout_weight="1"
                                    android:layout_height="match_parent"
                                    android:background="@drawable/star"
                                    android:paddingLeft="5dp" />

                                <Button
                                    android:id="@+id/share"
                                    android:layout_width="0dp"
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:text="Share"
                                    android:textColor="@color/background_material_light"/>
                                <Button
                                    android:id="@+id/comment"
                                    android:layout_width="0dp"
                                    android:layout_weight="1"
                                    android:layout_height="wrap_content"
                                    android:text="Comment"
                                    android:textColor="@color/background_material_light"/>
                -->
            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

isVoter和viewHolder不应该是Adapter中的全局变量。意思是,你实际上并没有改变newsItemArray.get(position)返回的任何成员.isVoter()

尝试将位置添加到ViewHolder对象。然后将ViewHolder对象传递给viewHolder.vote.setOnClickListener的构造函数。在OnClickListener中,您可以调用

isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);