Reputation: 1519
I have done add and remove tabs, with custom tab view, which works fine. I have the same view for all tabs, added in viewpager. I have click listeners for some view in the layout, loaded in viewpager. But click listener worked on last added tab, not working on other tabs.
Activity
public class Test extends BaseActivity implements View.OnClickListener {
private TextView flatRateTextView, timeMaterialTextView,
totalWorkOrderValueTextView;
private LinearLayout bidTimeMaterialParentLayout, bidTaxTotalParentLayout;
boolean isFlatRateClicked, isTimeMaterialClicked;
private ViewPager pager = null;
private MainPagerAdapter pagerAdapter = null;
TabLayout tabLayout;
int i;
private void initViews() {
i++;
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pagerAdapter);
appendViewToViewPager(0);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicator(null);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabLayout.getLayoutParams();
params.setMargins(widthSpace, widthSpace, widthSpace, 0);
tabLayout.setLayoutParams(params);
// tabLayout.setC(v0);
tabLayout.setupWithViewPager(pager);
setupTabIcons();
//Dynamic job Bit & Material Start
TextView totalWorkOrderTextView = findViewById(R.id.totalWorkOrderTextView);
totalWorkOrderValueTextView = findViewById(R.id.totalWorkOrderValueTextView);
TextView addJobTextView = findViewById(R.id.addJobTextView);
TextView submitTextView = findViewById(R.id.submitTextView);
submitTextView.setOnClickListener(this);
addJobTextView.setOnClickListener(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// ... do other initialization, such as create an ActionBar ...
initViews();
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(pager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
// updateTabs();
int numTab = tab.getPosition();
Log.e("numTab >", numTab + "");
pager.setCurrentItem(numTab);
}
});
}
private void setupTabIcons() {
for (int j = 0; j < i; j++) {
Log.e("j val>>", j + "");
View rootView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.job_dynamic_header_layout, null);
TextView tabOne = rootView.findViewById(R.id.jobHeaderTextView);
int tmp = j + 1;
tabOne.setText("Job" + tmp);
ImageView closeImageView = rootView.findViewById(R.id.closeImageView);
closeImageView.setId(tmp);
closeImageView.setOnClickListener(onClickListener);
tabLayout.getTabAt(j).setCustomView(rootView);
}
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
int tabIdToRemove = v.getId();
tabIdToRemove = tabIdToRemove - 1;
if (tabIdToRemove < 1) {
Toast.makeText(Test.this, "Couldn't remove first tab", Toast.LENGTH_SHORT).show();
} else {
i--;
Toast.makeText(Test.this, "Removed tab" + tabIdToRemove, Toast.LENGTH_SHORT).show();
pagerAdapter.removeView(pager, tabIdToRemove);
pagerAdapter.notifyDataSetChanged();
setupTabIcons();
}
break;
}
}
};
private void appendViewToViewPager(int position) {
LayoutInflater inflater = Test.this.getLayoutInflater();
NestedScrollView v = (NestedScrollView) inflater.inflate(R.layout.vendor_submit_bit_view_pager_layout, null);
flatRateTextView = v.findViewById(R.id.flatRateTextView);
timeMaterialTextView = v.findViewById(R.id.timeMaterialTextView);
bidTimeMaterialParentLayout = v.findViewById(R.id.bidTimeMaterialParentLayout);
bidTaxTotalParentLayout = v.findViewById(R.id.bidTaxTotalParentLayout);
flatRateTextView.setTag(position);
timeMaterialTextView.setTag(position);
flatRateTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isFlatRateClicked = true;
isTimeMaterialClicked = false;
bidTimeMaterialParentLayout.setVisibility(View.GONE);
bidTaxTotalParentLayout.setVisibility(View.VISIBLE);
flatRateTextView.setBackground(ContextCompat.getDrawable(Test.this, R.drawable.bottom_red_line));
timeMaterialTextView.setBackground(null);
}
});
timeMaterialTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isFlatRateClicked = false;
isTimeMaterialClicked = true;
totalWorkOrderValueTextView.setText("$0");
bidTimeMaterialParentLayout.setVisibility(View.VISIBLE);
bidTaxTotalParentLayout.setVisibility(View.GONE);
timeMaterialTextView.setBackground(ContextCompat.getDrawable(Test.this, R.drawable.bottom_red_line));
flatRateTextView.setBackground(null);
}
});
pagerAdapter.addView(v, position);
pagerAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.backImageView:
Intent newIntent = new Intent(Test.this, VendorHomeActivity.class);
startActivity(newIntent);
break;
case R.id.addJobTextView:
i++;
int tmp = i - 1;
appendViewToViewPager(tmp);
setupTabIcons();
break;
case R.id.submitTextView:
break;
}
}
}
test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white_color"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/dynamicJobLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layoutTotalWorkOrder"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/layoutTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:background="@android:color/transparent"
android:state_selected="false"
app:tabBackground="@drawable/job_tab_selector"
app:tabGravity="fill"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_color"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/layoutTotalWorkOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/layBottom"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/totalWorkOrderTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|end"
android:text="@string/total_work_order_cost"
android:textColor="@color/dark_grey_color"
android:textSize="@dimen/text_size_16"
/>
<TextView
android:id="@+id/totalWorkOrderValueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="$0"
android:textColor="@color/red_color"
android:textSize="@dimen/text_size_16"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/layBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<TextView
android:id="@+id/addJobTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/grey_cornor_stroke_bg"
android:gravity="center"
android:text="@string/add_job_label"
android:textColor="@color/dark_grey_color"
android:textSize="@dimen/text_size_14"
/>
<TextView
android:id="@+id/submitTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/corner_5_dp_red_bg"
android:gravity="center"
android:text="@string/submit_label"
android:textColor="@color/white_color"
android:textSize="@dimen/text_size_14"
/>
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<include layout="@layout/vendor_job_work_order_view" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
job_dynamic_header_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/headerLay"
android:gravity="center_vertical"
>
<TextView
android:id="@+id/jobHeaderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size_14"
android:textColor="@color/white_color"
android:text="Job"/>
<ImageView
android:id="@+id/closeImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/white_cross_icon"/>
</LinearLayout>
vendor_submit_bit_view_pager_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/jobDescDetailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/custom_tab_layout_height"
android:background="@drawable/light_blue_white_cornor_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/flatRateParentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/flatRateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bottom_red_line"
android:gravity="center"
android:text="@string/flat_rate_label"
android:textColor="@color/black_color"
android:textSize="@dimen/text_size_small_12" />
<TextView
android:id="@+id/timeMaterialTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:singleLine="true"
android:text="@string/time_material_label"
android:textColor="@color/dark_grey_color"
android:textSize="@dimen/text_size_small_12" />
</LinearLayout>
<LinearLayout
android:id="@+id/bidTaxTotalParentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_10_cornor_bg"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/bidTimeMaterialParentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_10_cornor_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dynamicMaterialLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/materialLabelTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/materials_label"
android:textColor="@color/dark_grey_color"
android:textSize="@dimen/text_size_16" />
<TextView
android:id="@+id/addMaterialTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/add_material_label"
android:textColor="@color/red_color"
android:textSize="@dimen/text_size_16" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 606
Reputation: 16699
The problem is more architectural than code related. The answer to your question is pretty easy though.
You use the global variables for the View
s on every page of your ViewPager
. That means that each time new page is added - the old variables are erased and changed with the new ones from the new page.
I would suggest you to create global variables in your adapter MainPagerAdapter
to store page related view references and attach all the listeners in instantiateItem()
method of your adapter. To pass listener to your adapter from Activity
you may use some interface for example
interface MyClickListener{
void onClick(int pagePosition);
}
public class MainPagerAdapter extends PagerAdapter {
private TextView timeMaterialTextView;
MyClickListener listener;
...
public MainPagerAdapter(MyClickListener listener) {
this.listener = listener;
}
...
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = ...;
timeMaterialTextView = view.findViewById(R.id.timeMaterialTextView);
timeMaterialTextView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
...
listener.onClick(position);
...
}
});
container.addView(view);
return view;
}
...
}
public class Test extends BaseActivity implements MyClickListener {
private void initViews() {
...
pagerAdapter = new MainPagerAdapter(this);
...
}
void onClick(int position) {
/// do all the stuff you need to do here
}
}
It is not an exact answer, it rather depicts how your code structure should look like, but, I hope, you'll grasp further.
Hope it helps.
Upvotes: 3