【发布时间】:2018-01-17 19:16:10
【问题描述】:
活动类:
public class ScrollingActivity extends AppCompatActivity {
ViewPager myPager;
ImageButton leftBtn, rightBtn;
RecyclerView myList;
private List<String> _images;
GalleryPagerAdapter _adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
myPager = (ViewPager) findViewById(R.id.pager);
leftBtn = (ImageButton) findViewById(R.id.left_nav);
rightBtn = (ImageButton) findViewById(R.id.right_nav);
myList = (RecyclerView) findViewById(R.id.recyclerviewFrag);
_images = Arrays.asList(getResources().getStringArray(R.array.user_photos));
Assert.assertNotNull(_images);
_adapter = new GalleryPagerAdapter(this);
myPager.setAdapter(_adapter);
myPager.setOffscreenPageLimit(4); // how many images to load into memory
_adapter.notifyDataSetChanged();
HorizontalAdapter horizontalAdapter = new HorizontalAdapter(_images);
LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
myList.setLayoutManager(horizontalLayoutManagaer);
myList.setAdapter(horizontalAdapter);
horizontalAdapter.notifyDataSetChanged();
leftBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = myPager.getCurrentItem();
if (tab > 0) {
tab--;
myPager.setCurrentItem(tab);
} else if (tab == 0) {
myPager.setCurrentItem(tab);
}
}
});
// Images right navigatin
rightBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tab = myPager.getCurrentItem();
tab++;
myPager.setCurrentItem(tab);
}
});
// MenuScroll.setSmoothScrollingEnabled(true);
myPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
myList.setPadding(5, 5, 5, 5);
LinearLayout llSelect;
for (int i = 0; i < _images.size(); i++) {
llSelect = ((LinearLayout) myList.getChildAt(i));
if (i == position) {
llSelect.setBackgroundColor(Color.parseColor("#3fa9f5"));
} else {
llSelect.setBackgroundColor(Color.parseColor("#ffffff"));
}
myList.scrollToPosition(position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class GalleryPagerAdapter extends PagerAdapter {
Context _context;
LayoutInflater _inflater;
GalleryPagerAdapter(Context context) {
_context = context;
_inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return _images.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = _inflater.inflate(R.layout.pager_gallery_item, container, false);
container.addView(itemView);
// Get the border size to show around each image
//int borderSize = _thumbnails.getPaddingTop();
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 260);
params.setMargins(3, 3, 3, 3);
final ImageView thumbView = new ImageView(_context);
thumbView.setTag(position);
thumbView.setScaleType(ImageView.ScaleType.CENTER_CROP);
thumbView.setLayoutParams(params);
thumbView.setMinimumHeight(260);
thumbView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
/* for (int j=0;j<_thumbnails.getChildCount();j++)
{
if (_thumbnails.getChildAt(j).getBackground()!=null) {
_thumbnails.getChildAt(j).setBackground(null);
}
}
Drawable highlight = getResources().getDrawable( R.drawable.border);
_thumbnails.getChildAt(position).setPadding(4,4,4,4);
_thumbnails.getChildAt(position).setBackground(highlight);*/
myPager.setCurrentItem(position);
}
});
final ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
Glide.with(ScrollingActivity.this).load("" + _images.get(position))
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(imageView);
imageView.setOnClickListener(new OnImageClickListener(position));
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
private class OnImageClickListener implements View.OnClickListener {
int myPostion;
// constructor
OnImageClickListener(int position) {
this.myPostion = position;
}
@Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<>(_images);
}
}
class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.MyViewHolder> {
private List<String> horizontalList;
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imgCards;
MyViewHolder(View view) {
super(view);
imgCards = (ImageView) view.findViewById(R.id.imgDisplay);
}
}
HorizontalAdapter(List<String> horizontalList) {
this.horizontalList = horizontalList;
}
@Override
public HorizontalAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_thumb_image, parent, false);
return new HorizontalAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final HorizontalAdapter.MyViewHolder holder, final int position) {
Glide.with(ScrollingActivity.this).load("" + horizontalList.get(position))
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(holder.imgCards);
holder.imgCards.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myPager.setCurrentItem(position);
}
});
}
@Override
public int getItemCount() {
return horizontalList.size();
}
}
}
布局文件:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="240dp"
android:background="@drawable/placeholder"
android:scaleType="fitXY"
android:clipToPadding="false"/>
<ImageButton
android:id="@+id/left_nav"
android:layout_width="30dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:background="#80ffffff"
app:srcCompat="@drawable/arrow_right"
android:padding="5dp"
android:layout_marginTop="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:id="@+id/right_nav"
android:layout_width="30dp"
android:layout_height="50dp"
android:background="#80ffffff"
android:scaleType="centerInside"
android:padding="5dp"
android:layout_centerVertical="true"
app:srcCompat="@drawable/arrow_left"
android:layout_alignTop="@+id/left_nav"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerviewFrag"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:layout_height="wrap_content"
android:layout_below="@+id/pager"
android:paddingTop="1dp"/>
</RelativeLayout>
拇指布局文件:
<LinearLayout
android:id="@+id/llSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgDisplay"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="1dp"
android:scaleType="centerCrop"
android:src="@drawable/placeholder" />
</LinearLayout>
pager_gallery_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:id="@+id/image" />
</LinearLayout>
work for only 4 thumb`` images
我需要在寻呼机视图中显示任何图像,并在 Thumb 视图图像中选择相关图像的边框。
用于图像滑动的 ViewPager 和 RecycleView(viewPager) 图像的缩略图 (RecycleView)。当显示相关图像时,我会显示选择的 ThumbImage。
【问题讨论】:
-
超过 4 张图像 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.LinearLayout.setBackgroundColor(int)'
-
将 setOffscreenPageLimit 从 4 更改为 _images.size()
-
嘿,你在吗?如果还是找不到解决办法,我会写代码
-
@jignesh 是的,我也试试这个。但仍然有同样的问题。
-
@propoLis 没有朋友,我仍然没有找到解决方案。请帮忙。