如何创建滚动到所选位置的回收站视图

时间:2017-10-18 14:31:58

标签: android android-recyclerview onclicklistener scrollable

我正在尝试制作应用程序,当我们点击回收站视图中的特定项目时,它应该启动一个带有回收站视图的片段,以便在我们选择显示所选帖子的帖子时显示屏幕中心的所选项目,如instagram网格视图中心的详细信息,我们也可以滚动到其他帖子

这是我的网格适配器

public class PostImageAdapter extends RecyclerView.Adapter<PostViewHolder> {

private Context context;
private List<Post> posts;
private OnGridItemSelected onGridItemSelected;

public PostImageAdapter(Context context) {
    this.context = context;
    posts = new ArrayList<>();
    onGridItemSelected = (OnGridItemSelected) context;
}

@Override
public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.layout_squareimage, parent, false);
    return new PostViewHolder(view);
}

@Override
public void onBindViewHolder(PostViewHolder holder, final int position) {

    Post post = posts.get(position);
    Glide.with(context).load(post.getImagePath())
            .error(R.drawable.ic_refresh)
            .placeholder(R.color.grey_3)
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .into(holder.imageView);

    final int adapterPosition = holder.getAdapterPosition();

    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "Adapter Position is" + adapterPosition , Toast.LENGTH_SHORT).show();
            onGridItemSelected.onItemSelected(adapterPosition);
        }
    });
}

public void addPaths(List<Post> paths) {
    int startIndex = posts.size();
    posts.addAll(paths);
    notifyItemRangeInserted(startIndex, posts.size());
}

@Override
public int getItemCount() {
    return posts.size();
}

我的网格片段

public void setupGrid() {
    mRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
    final PostImageAdapter adapter = new PostImageAdapter(context);
    mRecyclerView.setAdapter(adapter);
    mRecyclerView.setHasFixedSize(true);
    FirebaseQuery.getAllPost().addListenerForSingleValueEvent(new ValueEventListener() {
        final ArrayList<Post> posts = new ArrayList<>();

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            posts.clear();
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                posts.add(ds.getValue(Post.class));
            }
            adapter.addPaths(posts);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

发布详细信息片段

public class PostDetailsFragment extends Fragment {
private static final String TAG = PostDetailsFragment.class.getSimpleName();

@BindView(R.id.postDetailsRecyclerView)
RecyclerView mRecyclerView;
private Unbinder unbinder;
private Context context;
private int i;

public PostDetailsFragment() {
    super();
    setArguments(new Bundle());
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_post_details, container, false);
    unbinder = ButterKnife.bind(this, view);
    context = getActivity();
    try {
        i = getPosition();
    } catch (NullPointerException e) {
        Log.d(TAG, "onCreateView:" + e.getMessage());
    }
    return view;
}

@Override
public void onStart() {
    super.onStart();
    setRecyclerView(i);
}

private int getPosition() {
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        return bundle.getInt(context.getString(R.string.key));
    } else {
        return 0;
    }
}

public void setRecyclerView(int position) {

    final PostImageAdapter adapter = new PostImageAdapter(context);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    linearLayoutManager.scrollToPosition(position);
    mRecyclerView.setAdapter(adapter);
    FirebaseQuery.getAllPost().addListenerForSingleValueEvent(new ValueEventListener() {
        final ArrayList<Post> posts = new ArrayList<>();

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            posts.clear();
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                posts.add(ds.getValue(Post.class));
            }
            adapter.addPaths(posts);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

@Override
public void onDestroy() {
    super.onDestroy();
    unbinder.unbind();
}

}

我试图传递适配器位置并设置适配器以滚动/启动该位置,它似乎不起作用PLZ帮助我创建像instagram这样的系统,它以线性布局显示详细信息。 。

0 个答案:

没有答案