setVisibility 和 setClickable 不适用于 ViewPager 中的 TextView(滑动),但 setText 是

setVisibility & setClickable not working for TextView in ViewPager (swipes), but setText is

我最近才了解 JAVA 和 Android 编程,所以请耐心等待。

Context:我用一个 XML 布局制作了一个选项卡 Activity(使用 ViewPager 和 SectionsPagerAdapter)。为此,我使用了来自 Android Studio 的标准 Activity 片段。对于不同的页面(滑动),只有某些 TextView 的值不同。我已经使用 onViewCreated 覆盖进行了设置。这在初始化时工作正常,但现在我想在用户交互 (onClick) 时更新和隐藏 TextView。这需要跟踪 PlaceholderFragment 中的页面视图,这是我通过我在这个论坛上阅读的 setTag 和 findViewWithTag 方法完成的,即我做了这个覆盖:

在 PlaceholderFragment 内:

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

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        //added to keep reference to View
        int position = getArguments().getInt(ARG_SECTION_NUMBER);
        rootView.setTag(position);

        return rootView;
    }

现在我尝试更新三个 TextView,包括在单击 TextView textViewStart 时单击的那个:

在主类中:

public void onClick (View clickview) {        
    Log.i("onClick called: ", Integer.toString(clickview.getId())); // this gives a valid number. probably different from TextView ID in mView defined below

    // get the current View from the ViewPager, stored using setTag
    final View mView = mViewPager.findViewWithTag(currentTab); //currentTab is a variable with the current selected Page in the ViewPager, made using an override in onTabSelected

    //Need to findIDs (for current tab page in ViewPager)
    final TextView tvStart = (TextView)mView.findViewById(R.id.textViewStart);
    final TextView tvPause = (TextView)mView.findViewById(R.id.textViewPause);
    final TextView tvStop = (TextView)mView.findViewById(R.id.textViewStop);
    //TextView tvStart = (TextView)findViewById(R.id.textViewStart); // this refers to another tab !  clickview.findViewById ONLY works for the actual object clicked

    switch (clickview.getId()) {
        case R.id.textViewStart: // START

            runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      //update controls
                      tvStart.setClickable(false);
                      tvStart.setVisibility(View.INVISIBLE);
                      tvStart.setTextColor(Color.parseColor("#888888"));//#RRGGBB // = grey
                      tvPause.setClickable(true);
                      tvPause.setVisibility(View.VISIBLE);
                      tvStop.setClickable(true);
                      tvStop.setVisibility(View.VISIBLE);
                      //NOTE this invalidates the textViews, BUT does not redraw the View, which only happens after onClick thread is finished, so what to do??

                  }

            });

            // mViewPager.setCurrentItem(currentTab-1); //also does not update View

            //start timer
            timerStatus = 1;
            startTimer(currentTab); //this is another method, which starts a timer in the same View, this updates a ProgressBar which works.
    }
}

为了以防万一,TextViews 在XML 中定义如下:

fragment_main.xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/Start"
    android:id="@+id/textViewStart"
    android:textSize="30dp"
    android:textColor="#fff"
    android:onClick="onClick"
    android:clickable="true"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    />

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/Pause"
    android:id="@+id/textViewPause"
    android:textSize="30dp"
    android:textColor="#fff"
    android:onClick="onClick"
    android:clickable="false"
    android:layout_below="@+id/view"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:visibility="invisible" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/Stop"
    android:id="@+id/textViewStop"
    android:textSize="30dp"
    android:textColor="#fff"
    android:onClick="onClick"
    android:clickable="false"
    android:layout_below="@+id/view"
    android:layout_alignRight="@+id/tvTimerTotal"
    android:layout_alignEnd="@+id/tvTimerTotal"
    android:visibility="invisible" />

问题: 我根本无法让 TextViews 出现或消失,或者让它们(不可)点击。现在发生的事情是 TextViewStart (tvStart) 按预期变灰,但它不会消失并且仍然可以单击(我可以启动另一个 startTimer 实例)。此外,其他 TextViews tvPause 和 tvStop 不会出现。计时器(从主类的一个方法 startTimer() 开始)工作正常,并且还使用相同的 findViewWithTag 方法更新同一页面上的 ProgressBar。

所以奇怪的是,TextViews 的 ID 必须是正确的,因为 setText 和 setTextColor 可以工作,但是完全相同的对象上的 setVisibility 和 setClickable 不工作!

我第一次尝试没有运行 runOnUiThread,它的工作原理类似。我还尝试将所有内容都放在 synchronized Thread, but that has similar results too. I also tried to put the startTimer in a tvStop.post() { ... } 中,但这甚至没有启动计时器..

实在是看不懂了,我这里忽略了什么?

编辑:

这是我的完整 Activity(至少是 Framents 设置的一部分):

public class SomeActivity extends ActionBarActivity implements ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

//Peter: need access to current tab throughout activity
int currentTab;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_whatever);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_whatever, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.        
    mViewPager.setCurrentItem(tab.getPosition());

    // store current position in global variable
    currentTab = tab.getPosition()+1; // +1 (!)
    Log.i("TEST onTabSelected currentTab", Integer.toString(currentTab));
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).

        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        // DEFAULT: return 3;
        return getResources().getStringArray(R.array.whatever_sections).length;
    }

    @Override
    //PETER: this is the names of the pages
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        // DEFAULT OVERRIDE            
        String[] menuItems = getResources().getStringArray(R.array.whatever_sections);
        return menuItems[position];
    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */        
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_whatever, container, false);
        //Peter added to keep reference to View (THIS WORKS)
        int position = getArguments().getInt(ARG_SECTION_NUMBER);
        rootView.setTag(position);
        //Log.i("TEST onCreateView ARGS position: ",Integer.toString(position));

        return rootView;
    }

    //PETER: added, this is apparently the default for setup of the items View components
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        //default code:
        super.onViewCreated(view, savedInstanceState);

        TextView tvWork = (TextView)view.findViewById(R.id.tvWork);
        TextView tvPause = (TextView)view.findViewById(R.id.tvPause);

                tvWork.setText("0:10");
                tvPause.setText("4:00"); // 4 minutes
    }            

我发现了这个问题的问题。这不是 setVisibility 或 View 中的错误,而是初学者在 switch case 语句中的错误。我还有其他情况(未在我的 post 中的代码中显示),例如 Stop 和 Pause 实现,但没有将

 break;

在此代码中:

public void onClick (View clickview) throws InterruptedException {
    switch (clickview.getId()) {
        case R.id.textViewStart: // START
            tvStart.setVisibility(View.INVISIBLE);
        case R.id.textViewStop: // STOP
            tvStart.setVisibility(View.VISIBLE);
    } 
}

所以案例 fall through,并立即再次设置 TextView 可见。在每个案例中添加 "break" 后,它按预期工作:

public void onClick (View clickview) throws InterruptedException {
    switch (clickview.getId()) {
        case R.id.textViewStart: // START
            tvStart.setVisibility(View.INVISIBLE);
            break; //otherwise next cases will all be executed too!
        case R.id.textViewStop: // STOP
            tvStart.setVisibility(View.VISIBLE);
            break;
    } 
}