TextView setText and setVisibility not working in

2019-08-02 01:10发布

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.

Tried methods:

  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.

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:

<?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

<?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>

Please help me

4条回答
祖国的老花朵
2楼-- · 2019-08-02 01:45

try to setVisibility android:visibility="visible" or android:visibility="invisible" delete gone attribute it remove TextView.

<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="visible" //change here
    tools:text="11"
    tools:visibility="visible" />
查看更多
走好不送
3楼-- · 2019-08-02 01:45

Try using the below 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"
    android: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:text="11"/>
</LinearLayout>

Hope it helps!

查看更多
贼婆χ
4楼-- · 2019-08-02 01:55

Please use android:text instead tools:text

  1. tools:text="text" is used only for Android Studio layout preview (wont be visible while running the app)
  2. android:text="text" is used to set text to a a layout element, which you can see when the app is run
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-08-02 02:05

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());

          }
      }
}
查看更多
登录 后发表回答