关于水平循环器视图中的ProperScrolling

我有问题在水平回收视图中滚动。例如,如果我有10个项目,第一次,我能够滚动到最后一项,但如果我去第一项,然后再次滚动到最后一项,它没有移动到最后一项。

这个问题是否与滚动有关,如果是这种情况,我是否应该覆盖滚动。

这是我的片段类声明

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); mRecyclerView = (RecyclerView)v. findViewById(R.id.recycler_view); adapter = new MyRecyclerAdapter(getActivity()); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setAdapter(adapter); 

并在我的适配器

 public class MyRecyclerAdapter extends RecyclerView.Adapter { private Context mContext; JobSiteActivity mjobSiteActivity; ImageLoader loader = null; public MyRecyclerAdapter(Activity activity) { this.mContext = activity; mjobSiteActivity = (JobSiteActivity)activity; loader = mjobSiteActivity.getImageLoader(); } @Override public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_detail_fragment, null); CustomViewHolder viewHolder = new CustomViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(CustomViewHolder customViewHolder, int i) { //FeedItem feedItem = feedItemList.get(i); //Download image using picasso library /* Picasso.with(mContext).load(feedItem.getThumbnail()) .error(R.drawable.placeholder) .placeholder(R.drawable.placeholder) .into(customViewHolder.imageView);*/ customViewHolder.imageView.setImageUrl(Images.imageThumbUrls[i], loader); customViewHolder.imageView.setOnClickListener(clickListener); customViewHolder.imageView.setTag(customViewHolder); //Setting text view title } @Override public int getItemCount() { //return (null != feedItemList ? feedItemList.size() : 0); return Images.imageThumbUrls.length; } public class CustomViewHolder extends RecyclerView.ViewHolder { protected NetworkImageView imageView; public CustomViewHolder(View view) { super(view); this.imageView = (NetworkImageView) view.findViewById(R.id.imageView); } } View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View view) { CustomViewHolder holder = (CustomViewHolder) view.getTag(); int position = holder.getLayoutPosition(); int position1 = holder.getAdapterPosition(); Toast.makeText(mContext, "layoutPs"+position+"adapPos"+position, Toast.LENGTH_SHORT).show(); } }; 

}

我正在尝试使用V17 leanback附带的水平网格视图,它也扩展了recycleView,但我仍然面临着类似的问题,

如果我尝试在运行时将任何项目添加到回收器视图中,例如在拍摄照片时,它也没有被添加,它分配空间并且它是空的?在这个问题上很难理解或调试。

Plz试试这个

  LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); RecyclerView recyclerViewr = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(layoutManager); 

并在适配器中

  private class ImageCycleAdapter extends RecyclerView.Adapter { private Context context; private List ids; private RecyclerViewClickListener listner; private int focusedItem = 0; public ImageCycleAdapter(Context context, List urlsList, RecyclerViewClickListener listner) { this.context = context; this.ids = urlsList; this.listner = listner; } @Override public ImageCycleAdapter.ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_detail_fragment, null); CustomViewHolder viewHolder = new CustomViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ImageCycleAdapter.ImageViewHolder holder, int position) { customViewHolder.imageView.setImageUrl(Images.imageThumbUrls[i], loader); customViewHolder.imageView.setOnClickListener(clickListener); customViewHolder.imageView.setTag(customViewHolder); } private void tryMoveSelection(RecyclerView.LayoutManager lm, int position) { focusedItem = position; notifyItemChanged(focusedItem); lm.scrollToPosition(focusedItem); notifyDataSetChanged(); } @Override public int getItemCount() { return ids.size(); } public class ImageViewHolder extends RecyclerView.ViewHolder { protected NetworkImageView imageView; public ImageViewHolder(View v) { super(v); this.imageView = (NetworkImageView) view.findViewById(R.id.imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // do your action } }); } } } public interface RecyclerViewClickListener { public void recyclerImageClicked(View v, int position); } 

尝试使用这样的layoutManager,可以帮到你

 LinearLayoutManager layoutManager=new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false); 

尝试这个

适配器

 public class CardAdapter extends RecyclerView.Adapter { ArrayList m_list = new ArrayList(); public CardAdapter(ArrayList m_list){ this.m_list = m_list; } @Override public int getItemCount() { return m_list.size(); } @Override public void onBindViewHolder(ViewHolder hv, int arg1) { UrlImageViewHelper.setUrlDrawable(hv.img, m_list.get(arg1), R.drawable.ic_launcher, new UrlImageViewCallback() { @Override public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) { } }); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) { View v; v = LayoutInflater.from(viewGroup.getContext()).inflate( R.layout.friend_recycler_item, viewGroup, false); ViewHolder hv = new ViewHolder(v); return hv; } class ViewHolder extends RecyclerView.ViewHolder { ImageView img; public ViewHolder(View arg1) { super(arg1); img = (ImageView) arg1.findViewById(R.id.img); } } } 

在活动中

 RecyclerView lstPacks = (RecyclerView) rowView.findViewById(R.id.lstPacks); lstPacks.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); lstPacks.setLayoutManager(holder.mLayoutManager); CardAdapter adapter1 = new CardAdapter(list); lstPacks.setAdapter(holder.adapter1); 

感谢您的帮助。在我的Volley库的NetworkImageView的情况下,通过给予预定义的宽度和高度,每个部分解决了部分解决了100 dp到子视图。而不是给出包装内容或匹配父级,给出预定义的宽度和高度使我工作:),不是一个正确的解决方案,但问题仍然存在与horizo​​ntalGridView(LeanBack库)