我已经实现了标签布局设计,我想在有人点击viewpager中的图片时切换到其他标签。我的研究得出结论,在viewpager适配器上注册了一个click事件但是我找不到从适配器中获取选项卡布局id的方法。这是我的view pager适配器代码:
public class BooksPagerAdapter extends PagerAdapter {
private Context mContext;
ViewPager viewPager;
private ViewPager pager = null;
ImageView image1;
private int images[] = {R.drawable.lagers11, R.drawable.stout11, R.drawable.malts11, R.drawable.lagers11, R.drawable.stout11, R.drawable.malts11};
public BooksPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
RelativeLayout relativeLayout = (RelativeLayout) ViewGroup.inflate(mContext, R.layout.tab_layout, null);
View itemView = inflater.inflate(R.layout.shop_books, container, false);
ImageView image1 = (ImageView) itemView.findViewById(R.id.image);
image1.setImageResource(images[position]);
itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//this will log the page number that was click
Toast.makeText(mContext, position + " pagess", Toast.LENGTH_SHORT).show();
}
});
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return (6);
}
@Override
public float getPageWidth(int position) {
return (0.3f);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}}
show_books layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:padding="2dp"
android:layout_height="100dp" />
</FrameLayout>
标签布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shattered"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/qb"
android:elevation="6dp"
app:tabGravity="fill"
app:tabIndicatorColor="#ffffff"
app:tabIndicatorHeight="3dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/tabSelectedTextColor"
app:tabTextColor="@color/tabTextColor">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpagertabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>
答案 0 :(得分:1)
简单,在
Document document = Jsoup.connect(url).get(); String title = document.select("td ul li b").first().text(); String content = document.select("td ul li").first().ownText(); Element linkEl = document.select("td ul li p a").first(); String href = linkEl.attr("href"); String link = linkEl.text();
图片中你应该试试这个
OnClick
答案 1 :(得分:0)
您可以使用EventBus
在您的活动或片段的子视图之间进行通信。首先定义一个事件:
public class ImageSelectedEvent {
int index;
public MessageEvent(int index) {
this.index = index;
}
}
注册此事件,如:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ImageSelectedEvent event) {
//TODO: make tab selected for event.index
}
为总线添加注册/取消注册代码:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
最后在点击图片时使用这样的东西:
itemView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int index = //get image index
EventBus.getDefault().post(new ImageSelectedEvent(index));
}
});
您可以在此处找到更多信息: