【问题标题】:Unable to interact with views in fragment activity无法与片段活动中的视图交互
【发布时间】:2017-02-19 09:47:01
【问题描述】:

您好,我被分配了在片段基本布局中创建内容的任务。但是,使用选项卡操作栏创建滑动活动后,我无法在每个给定选项卡中编辑片段布局。代码如下:

MainActivity.java :我用来引用片段布局的方法
(使用操作选项卡创建了一个默认的滑动活动)

package com.example.clement.testingfragments;


public class MainActivity extends AppCompatActivity {


 private SectionsPagerAdapter mSectionsPagerAdapter;
 private ViewPager mViewPager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    // action bar
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    //manage the fractions
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction =
            fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, new BlankFragment());
    fragmentTransaction.commit();


}


@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_main, 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);
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    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;
    }

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

        if(getArguments().getInt(ARG_SECTION_NUMBER) == 1) {

            View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
            return rootView;
        }else {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;

        }
    }
}

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.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }
  }
}

然后我创建了 BlankFragment.java : (默认创建一个fragment基本布局)

public class BlankFragment extends Fragment {



TextView mtv;
public BlankFragment() {
    // Required empty public constructor
}





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

    Log.i("hello","hello");

    View viewRoot = inflater.inflate(R.layout.fragment_blank, container, false);

        mtv = (TextView)viewRoot.findViewById(R.id.testy);
        mtv.setText("hello");
        return viewRoot;


     }

   }

问题在于我无法更改 fragment_blank.xml 中的文本视图。我在整个互联网上进行了研究,给出的建议是使用 getActivity()、onViewCreated()、Onstart() 创建 void 方法。所有这些似乎都不起作用。

我已经包含了一个 if else 循环,以在该方法有效的情况下返回 null,但它仍然运行而不返回 null,我不知道为什么会发生这种情况。

我的 fragment_blank.xml:

<FrameLayout 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"
tools:context="com.example.clement.testingfragments.BlankFragment">

<TextView
    android:id="@+id/testy"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</FrameLayout>

如果需要更多信息,请告诉我。我真诚地寻求您的建议和意见。提前致谢!

【问题讨论】:

    标签: android android-layout android-fragments android-view android-inflate


    【解决方案1】:

    根据您的问题,您的 textview 在 basicfragmentlayout.xml 内,而您在 onCreateView 内膨胀 fragment_blank。您需要更改代码如下:

    MyActivity.java

    public class MyActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction =
                    fragmentManager.beginTransaction();
            fragmentTransaction.replace(android.R.id.content, new MyFragment());
            fragmentTransaction.commit();
        }
    }
    

    MyFragment.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MyFragment extends Fragment {
        View myView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            myView = inflater.inflate(R.layout.fragment_my, container, false);
    
            TextView mtv = (TextView) myView.findViewById(R.id.testy);
            mtv.setText("hello");
    
            return myView;
        }
    }
    

    fragment_my.java

    <FrameLayout 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">
    
        <TextView
            android:id="@+id/testy"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="TempText" />
    
    </FrameLayout>
    

    【讨论】:

    • 哦,该死的,很抱歉我为这个问题写了错误的 xml 文件。它已被编辑。请再读一遍
    • 发布您的 xml 并从问题中删除不必要的代码。
    • 您的代码似乎可以满足您的要求。问题似乎出在其他地方。我已经编辑了包含您想要的示例的答案。您可以发布更多代码以获得更多帮助。
    • 好吧,谢谢伙计,这似乎很有希望(:因为我接下来几天不在,我会尽快更新!
    • 嘿,伙计,我设法尝试了您给我的代码,并将片段管理器添加到主要活动中。我希望我做对了。从外观上看,代码似乎没有错误,但有些代码没有显示在 AVD 中的转换器。你认为还有什么问题?我尝试将它链接到布局页面到另一个 java 类以与之交互,但仍然没有任何反应。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 2012-07-26
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2014-09-29
    相关资源
    最近更新 更多