gpt4 book ai didi

android - 自定义 View 单击监听器,不适用于当前选项卡

转载 作者:行者123 更新时间:2023-11-29 16:28:38 25 4
gpt4 key购买 nike

我已经使用自定义选项卡 View 添加和删除选项卡,效果很好。我在 viewpager 中添加了所有选项卡的相同 View 。我在布局中有一些 View 的点击监听器,加载在 viewpager 中。但是点击监听器在最后添加的选项卡上工作,而不在其他选项卡上工作。

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>

最佳答案

这个问题更多的是体系结构而不是代码相关。不过,您的问题的答案很简单。

您在 ViewPager 的每个页面上使用 View 的全局变量。这意味着每次添加新页面时 - 旧变量将被删除并更改为新页面中的新变量。

我建议您在适配器 MainPagerAdapter 中创建全局变量以存储页面相关的 View 引用,并在适配器的 instantiateItem() 方法中附加所有监听器。要将监听器从 Activity 传递给您的适配器,您可以使用一些接口(interface),例如

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
}

}

这不是一个确切的答案,而是描述了您的代码结构应该是什么样子,但我希望您能进一步理解。

希望对您有所帮助。

关于android - 自定义 View 单击监听器,不适用于当前选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com