TextView setText和setVisibility在片段中不起作用 [英] TextView setText and setVisibility not working in fragment

查看:79
本文介绍了TextView setText和setVisibility在片段中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ControllerFragment,其中有3个子片段.通过ViewPager在tabLayout中提供子片段更改.未发送的Apple和Banana徽章计数是可计算的变量值. unSendAppleCountTxtViewunSendBananaCountTxtView根据这些变量值进行更改.

I have ControllerFragment which has 3 child fragments. Children fragments change are provided in the tabLayout via ViewPager. Unsend Apple and Banana badge count are computable variable values. unSendAppleCountTxtView and unSendBananaCountTxtView are changed according to these variable values.

我的函数正在运行主线程.

My function is running main Thread.

尝试过的方法:

  1. 我控制了null检查,并且textView不为空
  2. 在这些线程中使用了
  3. setUnSendAppleCount函数(runOnUIThread,new Handler(),new Handler(Looper.getMainLooper()))
  4. 我已经尝试过AsyncTask,而且在postExecute中也无法使用setView和setText.
  5. 最后我扫描了所有相关问题并进行了尝试.
  1. I have controlled null check and textView isn't null
  2. setUnSendAppleCount function is taken in these threads (runOnUIThread, new Handler(), new Handler(Looper.getMainLooper()))
  3. I have tried AsyncTask and not working setView and setText in postExecute too.
  4. And finally I scan all related questions and tried.

所有方法均无效. TextView为空值.

All methods didn't work. TextView is empty value.

public class ControllerFragment extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.controller_fragment, viewGroup, false);

        tabs = ViewUtil.findById(rootView, R.id.tabs);
        viewPager = ViewUtil.findById(rootView, R.id.controllerViewPager);

        setFragments();
        setupViewPager(viewPager);
        tabs.setupWithViewPager(viewPager);

        setupTabIcons(viewGroup);

        return rootView;
      }

       private void setupTabIcons(ViewGroup viewGroup) {

        LinearLayout appleListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
        RelativeLayout pearListTab = (RelativeLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text, viewGroup, false);
        LinearLayout bananaListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);

        unSendAppleCountTxtView = appleListTab.findViewById(R.id.badgeView);
        unSendBananaCountTxtView = bananaListTab.findViewById(R.id.badgeView);

        TextView appleTextView = appleListTab.findViewById(R.id.textView);
        TextView pearTextView = pearListTab.findViewById(R.id.textView);
        TextView bananaTextView = bananaListTab.findViewById(R.id.textView);

        setUnSendAppleCount(2);  //....this function is not working
        setUnSendBananaCount(2); //.....this function is not working

        bananaTextView.setText(getString(R.string.bananas));
        tabs.getTabAt(0).setCustomView(bananaListTab);

        appleTextView.setText(getString(R.string.apples));
        tabs.getTabAt(1).setCustomView(appleListTab);

        pearTextView.setText(getString(R.string.pears));
        tabs.getTabAt(2).setCustomView(pearListTab);
    }

    private void setupViewPager(ViewPager viewPager) {

        TabsViewPagerAdapter adapter = new TabsViewPagerAdapter(getFragmentManager());
        adapter.addFrag(bananaFragment, getString(R.string.bananas));
        adapter.addFrag(appleFragment, getString(R.string.apples));
        adapter.addFrag(pearFragment, getString(R.string.pears));
        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(1);
    }

    private void setUnSendAppleCount(int unSendCount) {

        if (unSendAppleCountTxtView != null) {

         if (unSendCount > 0) {

         unSendAppleCountTxtView.setVisibility(View.VISIBLE);
         unSendAppleCountTxtView.setText(String.format(getMyLocale(), "%d", unSendCount));

         } else {
         unSendAppleCountTxtView.setVisibility(View.GONE);
         }
      }
   }
}

controller_fragment xml:

controller_fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/tab_unpressed_color">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:tabGravity="fill"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/white"
                app:tabTextColor="@color/tabColor" />

        </FrameLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/controllerViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/app_bar_layout" />

</RelativeLayout>

tab_title_text_and_badge.xml

tab_title_text_and_badge.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:weightSum="2"
    android:orientation="horizontal"
    tools:background="@color/Black">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center"
        android:layout_gravity="center"
        android:maxLines="1"
        android:layout_weight="1.99"
        android:textColor="@color/white"
        android:textSize="@dimen/infoTitle"/>

    <TextView
        android:id="@+id/badgeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start|center"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_weight="0.01"
        android:background="@drawable/tab_controller_badge"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="@dimen/miniTextSize"
        android:visibility="gone"
        tools:text="11"
        tools:visibility="visible" />

</LinearLayout>

请帮助我

推荐答案

我猜是不同的线程问题.我已经尝试过使用eventBus进行将数据传递到片段的活动,此问题得以解决.

I guess different thread problem. I have tried that eventBus for activity passing data to fragment and it solved.

  ControllerFragment(){

        @Override
        public void onStart() {
            super.onStart();

            if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);

        }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

   @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
   public void onEventMain(ControllerEvent event) {

      if (event.getSelectedEventType() == ControllerEvent.tabControllerEventTypes.refreshCount.ordinal()) {

          setUnSendAppleCount(event.getAppleCount());

          }
      }
}

这篇关于TextView setText和setVisibility在片段中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆