从活动

时间:2017-03-25 21:16:06

标签: android listview android-fragments android-tablayout

我正在处理的应用在TabLayout中有3个标签。每个都包含一个列表视图。另外我使用FragmentStatePageAdapter和自定义适配器。当您单击ListView中的项目时,它会启动一个活动。在活动中完成任务后,它会更新一些全局数组数据。接下来,用户可以点击手机上的后退按钮返回。这就是问题所在。

现在Tab1永远不会改变,我后来希望实现返回将它设置为Tab1。这是后来的担心。问题是中间选项卡不会重新加载。我理解原因是因为中间选项卡已经创建,所以在视觉上滑动效果看起来很准确。 Tab3将来自Tab1,因为它尚未加载。

我尝试了各种监听器,在tablayout Fragment中使用onResume。我的代码中的任何地方都没有notifydatasetchange,并且无法实现这一点。我认为我已经以某种方式实现了一些错误并超越了正确的解决方案,因为我已经尝试了很多这样的事情。

以下是一些有助于了解我如何设置所有内容的代码。

MainActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 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(container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(0);

    //more

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setScrollPosition(0, 0f, true);

}

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

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
protected Context mContext;

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).
    int number = position;
    switch (number) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return TabFragment1.newInstance();
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return TabFragment2.newInstance();
        case 2: // Fragment # 1 - This will show SecondFragment
            return TabFragment3.newInstance();
        default:
            return null;
    }

    // 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 "All";
        case 1:
            return "Unfinished";
        case 2:
            return "Completed";
    }
    return null;
}}

MyAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
protected Context mContext;

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).
    int number = position;
    switch (number) {
        case 0: // Fragment # 0 - This will show FirstFragment
            return TabFragment1.newInstance();
        case 1: // Fragment # 0 - This will show FirstFragment different title
            return TabFragment2.newInstance();
        case 2: // Fragment # 1 - This will show SecondFragment
            return TabFragment3.newInstance();
        default:
            return null;
    }

    // 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 "All";
        case 1:
            return "Unfinished";
        case 2:
            return "Completed";
    }
    return null;
}}

Fragment1 (所有三个片段完全相同)

public class TabFragment1 extends android.support.v4.app.Fragment {

public ArrayList<String> mylist1 = new ArrayList<>();

// newInstance constructor for creating fragment with arguments
public static TabFragment1 newInstance() {
    TabFragment1 fragmentFirst = new TabFragment1();
    Bundle args = new Bundle();
    //pass a list or something?
    fragmentFirst.setArguments(args);
    return fragmentFirst;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mylist1.clear();
    for (int i = 0; i < arrayInt.length; i++) {

            mylist1.add(array[i]);

    }


    final ListView listView = (ListView) rootView.findViewById(R.id.text_label);

    MyAdapter adapter = new MyAdapter(getContext(), mylist1);

    listView.setAdapter(adapter);

    //Click on Item
    //selecting items
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //goes to new activity passing the item name
            Intent intent = new Intent(view.getContext(), MapsActivity.class);
            Bundle b = new Bundle();

            //get text for current item
            //String textGet = listView.getItemAtPosition(position).toString();
            String textGet = mylist1.get(position);
            //put text into a bundle and add to intent
            intent.putExtra("text", textGet);

            //get position to carry integer
            intent.putExtra("position", position);

            intent.putExtras(b);

            //begin other activity
            startActivity(intent);
        }
    });

    return rootView;
}

这是一个较旧的尝试通知数据集已更改但没有运气。我把它放在碎片本身。

 public void onResume(){
    super.onResume();

  Log.e("Frag2", "Refresh Test");

    // a check so it doesnt run first time starting app and crashing
    if (g != 0) {
        updateData();
    }
    g = 1;

}

private void updateData(){
    Log.e("Frag2", "Refresh Test");
    this.mylist2.clear();
    for (int i = 0; i < arrayInt.length; i++) {
        if (arrayInt[i] == 1) {
            this.mylist2.add(array[i]);
        }
    }

    adapter = new MyAdapter(getContext(), this.mylist2);
    adapter.notifyDataSetChanged();

}

0 个答案:

没有答案