[英]How to Get PagerAdapter item Id for Horizontal ViewPager?
我已经实现了带有自定义 PagerAdapter 的 Horizontal ViewPager,就像附加在屏幕上一样。 我想要实现的是从 list 中获取 First item 的 Id 。 这是迄今为止所有内容的代码。
寻呼机适配器:
private static final String TAG = ViewPagerAdapter.class.getSimpleName();
private Context context;
private LayoutInflater layoutInflater;
private ArrayList<QuickActionItem> quickActionItems;
public ViewPagerAdapter(Context context, ArrayList<QuickActionItem> items) {
this.context = context;
this.quickActionItems = items;
}
@Override
public float getPageWidth(int position) {
return 0.25f;
}
@Override
public int getCount() {
return quickActionItems.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.item_page_slide, null);
ImageView actionImage = view.findViewById(R.id.img_action);
TextView actionText = view.findViewById(R.id.title_action_value);
actionImage.setImageResource(quickActionItems.get(position).getActionDrawableImage());
actionText.setText(quickActionItems.get(position).getActionName());
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (quickActionItems.get(position).getActionName().equals(context.getString(R.string.quick_add_property))) {
Log.d(TAG, "Add Property ");
} else if (quickActionItems.get(position).getActionName().equals(context.getString(R.string.quick_add_tenant))) {
Log.d(TAG, "Add Tenant ");
context.startActivity(targetIntent);
} else if (quickActionItems.get(position).getActionName().equals(context.getString(R.string.quick_add_income))) {
Log.d(TAG, "Add Income ");
context.startActivity(targetIntent);
} else if (quickActionItems.get(position).getActionName().equals(context.getString(R.string.quick_add_expense))) {
Log.d(TAG, "Add Expense ");
}
}
});
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
快速操作项
public class QuickActionItem {
private String actionName;
private int actionDrawableImage;
}
主片段
quickActionPager = fragmentView.findViewById(R.id.quick_actions_pager);
sliderDotsPanel = fragmentView.findViewById(R.id.SliderDots);
quickActionItems = new ArrayList<>();
quickActionItems.add(new QuickActionItem(getString(R.string.quick_add_property), R.drawable.ic_add_property));
quickActionItems.add(new QuickActionItem(getString(R.string.quick_add_tenant), R.drawable.ic_add_tenant));
quickActionItems.add(new QuickActionItem(getString(R.string.quick_add_income), R.drawable.ic_add_income));
quickActionItems.add(new QuickActionItem(getString(R.string.quick_add_expense), R.drawable.ic_add_expense));
viewPagerAdapter = new ViewPagerAdapter(getActivity(), quickActionItems);
quickActionPager.setAdapter(viewPagerAdapter);
我只分享 MainFragment 代码的必要部分,因为它很长,无论我想实现什么。
问题:
1) 第一个项目是否有可能获得任何类型的 ID? 在我的情况下,“添加属性”项目的 ID
2)如果可能,那么如何获取该 Id 以及我能否在其上调用 findViewById ?
如果您需要任何其他信息或代码,也请告诉我。
任何帮助,将不胜感激。
请尝试以下操作:
因此,您需要在 viewPager 中获取某个页面的布局,然后使用findViewById()
获取布局视图。 您可以通过将每个膨胀的布局放入TreeMap中的instantiateItem()
并在destroyItem()
销毁时从TreeMap 中删除布局来实现这一点。 然后你可以通过使用treeMap.get(position)
来获得一定的页面布局 下面是一个例子:
主活动类
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private ViewPager vp;
private ViewPagerAdapter viewPagerAdapter = null;
private Button b;
private List<String> data = new ArrayList<>();
private List<Integer> color = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/**
* use viewPagerAdapter.getLayout(int position) to get the layout for a certain page
* then use findViewById() to get a certain view inside that layout
*/
View layout = viewPagerAdapter.getLayout(vp.getCurrentItem());
if (layout != null) {
TextView tv = (TextView) layout.findViewById(R.id.tv);
tv.setTextColor(color.get(vp.getCurrentItem()));
}
}
});
color.add(Color.RED);
color.add(Color.GRAY);
color.add(Color.BLUE);
color.add(Color.YELLOW);
color.add(Color.MAGENTA);
color.add(Color.CYAN);
color.add(Color.LTGRAY);
color.add(Color.DKGRAY);
for(int i=0; i<8; i++){
data.add("Page " + (i+1));
}
viewPagerAdapter = new ViewPagerAdapter(MainActivity.this, data);
vp.setAdapter(viewPagerAdapter);
}
}
ViewPagerAdapter.class
public class ViewPagerAdapter extends PagerAdapter {
private static final String TAG = ViewPagerAdapter.class.getSimpleName();
private Context context;
private LayoutInflater layoutInflater;
private List<String> data;
private TreeMap<Integer, View> layout;
public ViewPagerAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
this.layout = new TreeMap<>();
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
@NonNull
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.page_item, container, false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(data.get(position));
/**
* put the inflated layout inside TreeMap with key being the position
*/
this.layout.put(position, view);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
/**
* remove a layout with key position
*/
this.layout.remove(position);
vp.removeView(view);
}
/**
* used to get a layout
* @return TreeMap
*/
public TreeMap<Integer, View> getLayout() {
return this.layout;
}
/**
*
* @param position
* @return View at position
*/
public View getLayout(int position) {
return this.layout.get(position);
}
}
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:id="@+id/vp">
</android.support.v4.view.ViewPager>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/b"
android:layout_weight="20"
android:text="Change Text View Color">
</Button>
</LinearLayout>
page_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/ll"
android:layout_marginStart="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@color/colorAccent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv"
android:gravity="center"
android:layout_gravity="center"
android:textSize="24sp"
android:textColor="@android:color/black">
</TextView>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.