Reputation: 1374
I have implemented Horizontal ViewPager with Custom PagerAdapter like attached in screen. What I'm trying to achieve is to get Id of First item from list . Here is code code for everything till now .
PagerAdapter:
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);
}
}
QuickActionItem
public class QuickActionItem {
private String actionName;
private int actionDrawableImage;
}
MainFragment
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);
I'm sharing only necessary part of code for MainFragment as it's very long & regardless of what I want to achieve.
Questions:
1) Is it possible that first Item get any sort of ID or not ? In my case Id for "Add Property" item
2) If possible then how to get that Id & Will I able to call findViewById on that ?
Also let me know if you need any other information or code.
Any help would be appreciated.
Upvotes: 1
Views: 981
Reputation: 1829
Try the following:
So, You need to get the layout of a certain page inside a viewPager and then,
use findViewById()
to get the layout views. You can achieve that by putting every inflated layout in instantiateItem()
inside a TreeMap and removing a layout from the TreeMap when destroyed in destroyItem()
. Then you can get a certain page layout by using treeMap.get(position)
Below is an example:
MainActivty.class
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);
}
}
activity_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>
Upvotes: 1