Android RecyclerView StaggeredGrid项目在滚动时改变位置

时间:2015-11-04 19:27:56

标签: android android-studio android-volley staggered-gridview

我的问题是,当我滚动列表时,项目会保持互换。我怎样才能确定他们的位置?

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="1sp"
    >


        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/network_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:visibility="gone" />


        <TextView
            android:id="@+id/author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:visibility="gone"
            android:padding="8dp" />

        <TextView
            android:id="@+id/subreddit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/network_image"
            android:visibility="gone"
            android:text="New Text" />

        <TextView
            android:id="@+id/article_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/card_view"
            android:layout_alignStart="@+id/card_view"
            android:layout_below="@+id/author"
            android:visibility="gone"
            android:text="New Text" />

</LinearLayout>

我将文本的可见性消失了,因为我认为图像是继续移动对象的图像。有什么办法可以解决这个项目的交换问题吗?

AdapterCode:

public class MyRecyclerAdapter extends RecyclerView.Adapter<ListViewRowHolder> {

private List<ListItems> listItemsList;
private Context mContext;
private ImageLoader mImageLoader;
private int focusedItem = 0;
String[] urls = {"http://i.telegraph.co.uk/multimedia/archive/03020/ANIMALS_GOOSE_3020723k.jpg","http://www.evolo.us/wp-content/uploads/2014/01/New-York-Vertical-Central-Park-Jeffrey-Lee-Rui-Liu-Tina-Qiu-01.jpg"};

public MyRecyclerAdapter(Context context,List<ListItems> listItemsList,ImageLoader imageLoader){
    this.listItemsList = listItemsList;
    this.mContext = context;
    this.mImageLoader = imageLoader;
}

@Override
public ListViewRowHolder onCreateViewHolder(final ViewGroup viewGroup, final int position){
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
    ListViewRowHolder holder = new ListViewRowHolder(v);


    holder.relativeLayout.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            TextView redditUrl = (TextView) v.findViewById(R.id.url);
            String postUrl = redditUrl.getText().toString();
            Intent intent = new Intent(mContext, WebViewActivity.class);
            intent.putExtra("url", postUrl);
            mContext.startActivity(intent);
        }
    });

    return holder;
}

@Override
public void onBindViewHolder(final ListViewRowHolder listViewRowHolder, int position) {
    ListItems listItems = listItemsList.get(position);
    listViewRowHolder.itemView.setSelected(focusedItem == position);

    listViewRowHolder.getLayoutPosition();

    Random rand = new Random();
    int n = rand.nextInt(2);

    Log.d("photos",listItems.getThumbnail()+" "+ listItems.getUrl());

    listViewRowHolder.thumbnail.setImageUrl(urls[n], mImageLoader);
    listViewRowHolder.thumbnail.setDefaultImageResId(R.drawable.image1);

    listViewRowHolder.title.setText(Html.fromHtml(listItems.getTitle()));
    listViewRowHolder.subreddit.setText(Html.fromHtml(listItems.getSubreddit()));
    listViewRowHolder.author.setText(Html.fromHtml(listItems.getAuthor()));
    listViewRowHolder.url.setText(Html.fromHtml(listItems.getUrl()));
}

public void clearAdapter(){
    listItemsList.clear();
    notifyDataSetChanged();
}

@Override
public int getItemCount(){
    return (null != listItemsList ? listItemsList.size() : 0);
}


}

2 个答案:

答案 0 :(得分:3)

这里已经回答:

StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
recyclerView.setLayoutManager(manager);

Android - StaggeredGrid items disposition 并在github项目Picasso/Glide-RecyclerView-StaggeredGridLayoutManager

答案 1 :(得分:-1)

不是一次又一次地创建imageloader对象而是在适配器构造函数中创建对象并使用此

listViewRowHolder.thumbnail.setImageUrl(urls[n], mImageLoader);

在onBindViewHolder中为什么你使用随机来获取网址使用固定网址并使用位置来获取

希望这能解决您的问题