过去两天我无法解决此问题。我已经使用两个标签和viewpager实现了DialogFragment
,刷卡工作正常,但TabLayout.OnTabSelectedListener
无效。请查看下面我已完成的代码段。
CheckOutDialogFragment布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/checkout_discount_header"
android:gravity="center"
android:orientation="horizontal">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:background="@color/white"
android:fillViewport="true"
app:tabBackground="@color/white"
app:tabIndicatorColor="@color/jio_main_color"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/jio_main_color"
app:tabTextAppearance="@style/BillingTabStyle"
app:tabTextColor="#FF8C8C8C" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tablayout"
android:alpha="0.2"
android:background="@color/greytext" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="@dimen/dp271"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
BaseDialogFragment代码
public abstract class BaseDialogFragment extends DialogFragment {
/*Properties*/
@Nullable
private BaseActivity mActivity;
/*Life-cycle methods*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof BaseActivity) {
BaseActivity mActivity = (BaseActivity) context;
this.mActivity = mActivity;
}
}
@Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
@Nullable
public BaseActivity getBaseActivity() {
return mActivity;
}
protected abstract void setUp(View view);
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/*the content*/
final RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
/*creating the fullscreen dialog*/
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
if (dialog.getWindow() != null) {
dialog.getWindow().setBackgroundDrawable(new
ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
dialog.setCanceledOnTouchOutside(true);
DisplayMetrics discountDisplaymetrics = new DisplayMetrics();
mActivity.getWindowManager()
.getDefaultDisplay().getMetrics(discountDisplaymetrics);
int w = discountDisplaymetrics.widthPixels;
if (!(dialog.isShowing())) {
dialog.show();
}
dialog.getWindow().setLayout(w, LinearLayout.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setGravity(Gravity.BOTTOM);
return dialog;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUp(view);
}
/*Dialog fragment show method*/
public void show(@NonNull FragmentManager fragmentManager, String tag) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment prevFragment = fragmentManager.findFragmentByTag(tag);
if (prevFragment != null) {
transaction.remove(prevFragment);
}
transaction.addToBackStack(null);
show(transaction, tag);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
CheckoutDiscountDialogFragment.java代码
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.Toast;
import com.jio.bapp.R;
import com.jio.bapp.checkout.model.CheckoutCartData;
import com.jio.bapp.checkout.util.CheckoutCalculation;
import com.jio.bapp.retailpos.billing.adapter.StartBillingTabPageAdapter;
import com.jio.bapp.view.HelveticaNeueRegularTextView;
public class CheckoutDiscountDialogFragment extends BaseDialogFragment{
/*Properties*/
public TabLayout mTablayout;
ViewPager mViewpager;
private View view;
/*Important method to get call for this fragment.*/
@NonNull
public static CheckoutDiscountDialogFragment newInstance(int type) {
CheckoutDiscountDialogFragment fragment = new CheckoutDiscountDialogFragment();
Bundle args = new Bundle();
args.putInt(BUNDLE_TYPE, type);
fragment.setArguments(args);
return fragment;
}
/*Fragment life-cycle method*/
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mType = getArguments() != null ? getArguments().getInt("type") : 1;
if (savedInstanceState != null) {
mType = savedInstanceState != null ? savedInstanceState.getInt(BUNDLE_TYPE) : 1;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.checkout_discount_dialog, container, false);
res = this.getResources();
mTablayout = (TabLayout) view.findViewById(R.id.tablayout);
mViewpager = (ViewPager) view.findViewById(R.id.viewpager);
mTablayout.addTab(mTablayout.newTab().setText("Amount"));
mTablayout.addTab(mTablayout.newTab().setText("Discount"));
mTablayout.getTabAt(0).setText("Amount"));
mTablayout.getTabAt(1).setText("Discount"));
//**********Adding divider between tabs*********
LinearLayout linearLayout = (LinearLayout) mTablayout.getChildAt(0);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(getResources().getColor(R.color.search_layout_border));//;Color(Color.GRAY);
drawable.setSize(3, 3);
drawable.setColor(Color.parseColor("#E8E8E8"));
linearLayout.setDividerPadding(20);
linearLayout.setDividerDrawable(drawable);
ChoDiscountTabFragmentAdapter adapter = new ChoDiscountTabFragmentAdapter(getChildFragmentManager(), mTablayout.getTabCount());
mViewpager.setAdapter(adapter);
/*If Uncomment this below line tab Title gets invisible. */
//mTablayout.setupWithViewPager(mViewpager);
mViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTablayout));
/*tried below two lines still its not working.*/
//mTablayout.setOnTabSelectedListener(tabSelectedListener(mViewpager));
//mTablayout.addOnTabSelectedListener(tabSelectedListener(mViewpager));
mTablayout.addOnTabSelectedListener(onTabSelectedListener);
/*Tried below but not working.*/
/* mTablayout.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTablayout.getTabAt(0).equals(getResources().getString(R.string.checkout_amount_discount_tab))) {
mViewpager.setCurrentItem(0);
Toast.makeText(getActivity(), "Tab 1", Toast.LENGTH_SHORT).show();
} else {
mViewpager.setCurrentItem(1);
Toast.makeText(getActivity(), "Tab 2", Toast.LENGTH_SHORT).show();
}
}
});*/
adapter.notifyDataSetChanged();
mViewpager.invalidate();
return view;
}
private TabLayout.OnTabSelectedListener tabSelectedListener(final ViewPager pager)
{
return new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
Toast.makeText(getActivity(), "Tab 1", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
public void show(FragmentManager fragmentManager, String tag) {
super.show(fragmentManager, tag);
}
@Override
protected void setUp(View view) {
this.view = view;
}
@NonNull
private TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(@NonNull TabLayout.Tab tab) {
mViewpager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
ChoDiscountTabFragmentAdapter的代码
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import com.jio.bapp.checkout.model.CheckoutCartData;
import com.jio.bapp.retailpos.billing.activity.LooseProductFragment;
import com.jio.bapp.retailpos.billing.activity.ScanProductFragment;
import com.jio.bapp.retailpos.billing.activity.StartBillingActivity;
import java.util.ArrayList;
import java.util.List;
public class ChoDiscountTabFragmentAdapter extends FragmentPagerAdapter {
private int mNumOfTabs;
private final CheckoutDiscountDialogFragment mContext;
private Fragment mCurrentFragment;
public ChoDiscountTabFragmentAdapter(FragmentManager fragmentManager, int NumOfTabs) {
super(fragmentManager);
this.mNumOfTabs = NumOfTabs;
this.mContext = mContext;
}
@Nullable
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CheckOutDiscountFragment amountFragment = CheckOutDiscountFragment.newInstance(0);
amountFragment.setCartData(updateAmountToCollectListener);
return amountFragment;
case 1:
CheckOutPercentageFragment percentageFragment = CheckOutPercentageFragment.newInstance(1);
percentageFragment.setCartData(updatePercentageToCollectListener);
return percentageFragment;
default:
return null;
}
}
// Returns total number of pages
@Override
public int getCount() {
return mNumOfTabs;
}
public Fragment getCurrentFragment() {
return mCurrentFragment;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
if (getCurrentFragment() != object) {
try {
if (null != mContext && null != mContext.mTablayout) {
mContext.mTablayout.setScrollPosition(position, 0f, true);
}
mCurrentFragment = ((Fragment) object);
if (object instanceof CheckOutDiscountFragment) {
CheckOutDiscountFragment f = (CheckOutDiscountFragment) object;
if (f != null) {
f.getCartData(cartDataIs.getCartId());
}
}else if(object instanceof CheckOutPercentageFragment) {
CheckOutPercentageFragment f = (CheckOutPercentageFragment) object;
if (f != null) {
f.getCartData(cartDataIs.getCartId());
}
}
}catch (Exception e){
e.printStackTrace();
}
}
super.setPrimaryItem(container, position, object);
}
}
CheckOutDiscountFragment代码
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jio.bapp.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
public class CheckOutDiscountFragment extends Fragment {
/*Properties*/
public static final String BUNDLE_TYPE = "type";
private View rootView;
public int mType;
private Unbinder unbinder;
private Resources res;
public CheckOutDiscountFragment() {
}
/* Creational */
@NonNull
public static CheckOutDiscountFragment newInstance(int type) {
CheckOutDiscountFragment fragment = new CheckOutDiscountFragment();
Bundle args = new Bundle();
args.putInt(BUNDLE_TYPE, type);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mType = getArguments() != null ? getArguments().getInt("type") : 0;
if (savedInstanceState != null) {
mType = savedInstanceState != null ? savedInstanceState.getInt("type") : 0;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.checkout_discount_amount_fragment, container, false);
res = this.getResources();unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
this.mListener = (updateDiscount) context; /*Activity communicator (Fragment - to - Activity)*/
} catch (@NonNull final ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement Listener");
}
}
}
我希望有人可以帮我解决这个问题,建议我在DialogFragment
代码中做错了。