简体   繁体   English

如何在不承载该片段的活动中刷新从 Listview 接收数据的片段中的 Textview

[英]How to refresh a Textview in a fragment which receives data from a Listview in an activity which doesn't host that fragment

I have created a bottomNavigation bar which consists of 5 Fragments, so once each tab is clicked it will switch from one fragment to another.我创建了一个包含 5 个片段的底部导航栏,因此一旦单击每个选项卡,它将从一个片段切换到另一个片段。

The question is:The second fragment (Search fragment) have 1 TextView with setOnClickListener so once it is been licked a layout activity will open on the top which includes a ListView to allow the user to select/click on a specific Item, so later on this selected item info should be displayed on that TextView within the(Search fragment).问题是:第二个片段(搜索片段)有 1 个带有 setOnClickListener 的 TextView,因此一旦它被舔,一个布局活动将在顶部打开,其中包括一个 ListView 以允许用户选择/单击特定项目,以便稍后此选定项目信息应显示在(搜索片段)内的该 TextView 上。

The issue is that this Textview won't be updated, unless I call the mainActivity to so all fragments in bottom Navigation bar will be updated问题是这个 Textview 不会更新,除非我调用 mainActivity 来更新底部导航栏中的所有片段

My question is how I can refresh that specific fragment without calling the MainActivity.我的问题是如何在不调用 MainActivity 的情况下刷新该特定片段。

 public class MainActivity extends AppCompatActivity {



final Fragment f1 = new HomeFragment();
final Fragment f2 = new SearchFragment();
final Fragment f3 = new CameraFragment();
final Fragment f4 = new ChatFragment();
final Fragment f6 = new LogginFragment();

final FragmentManager fm = getSupportFragmentManager();
Fragment active = f1;

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



    BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation);

    bnve.enableAnimation(false);
    bnve.enableShiftingMode(false);
    bnve.enableItemShiftingMode(false);
    bnve.setOnNavigationItemSelectedListener(navListener);

    if(SharePrefManager.getInstance(this).isLoggedin()){
        finish();
        startActivity(new Intent(this, SuccessActivity.class));
        return;
    }
    fm.beginTransaction().add(R.id.fragment_container, f6, "6").hide(f6).commit();
    //fm.beginTransaction().add(R.id.fragment_container, f5, "5").hide(f5).commit();
    fm.beginTransaction().add(R.id.fragment_container, f4, "4").hide(f4).commit();
    fm.beginTransaction().add(R.id.fragment_container, f3, "3").hide(f3).commit();
    fm.beginTransaction().add(R.id.fragment_container, f2, "2").hide(f2).commit();
    fm.beginTransaction().add(R.id.fragment_container, f1, "1").commit();



}



public BottomNavigationViewEx.OnNavigationItemSelectedListener navListener =
        new BottomNavigationViewEx.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {

                    case R.id.nav_home:
                        fm.beginTransaction().hide(active).show(f1).commit();
                        active = f1;
                        return true;

                    case R.id.nav_search:
                        fm.beginTransaction().hide(active).show(f2).commit();
                        active = f2;
                        return true;
                    case R.id.nav_camera:
                        fm.beginTransaction().hide(active).show(f3).commit();
                        active = f3;
                        return true;
                    case R.id.nav_chat:
                        fm.beginTransaction().hide(active).show(f4).commit();
                        active = f4;
                        return true;
                    case R.id.nav_account:
                        fm.beginTransaction().hide(active).show(f6).commit();
                        active = f6;
                        return true;

                }



                return false;

            }
        };

    }

------------------------SearchFragment Class---------------------------------- ------------------------SearchFragment 类------------------------ ----------

This is the search fragment which has the textView (Categories) which supposed to be updated/be refershed without calling the MainActivity这是搜索片段,其中包含应该在不调用 MainActivity 的情况下更新/引用的 textView(类别)

public class SearchFragment extends Fragment  {


 private Context mContext;
 TextView Categories;


static boolean status = false;
String SelectedItem;

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

    View v = inflater.inflate(R.layout.fragment_search,container,false);
    Categories = (TextView) v.findViewById(R.id.categories);
    SelectedItem = DataHolder.getInstance().getItem();





        Categories.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, AllCateActivity.class));

            }
        });

        if(status){Categories.setText(SelectedItem);}


    return v;
}

public void ChangeStatus(Boolean status){

    this.status = status;
    }

@Override
  public void onAttach(Context context) {
    super.onAttach(context);
    mContext=context;
     }




 }

--------------------------DataHolder Class--------------------------------- --------------------------DataHolder 类-------------- -----------

This works as a design pattern to share arguments between the search fragment and Categories_Activity这作为一种设计模式在搜索片段和 Categories_Activity 之间共享参数

  public class DataHolder {

 private static DataHolder dataHolder = null;

 private DataHolder() {
     }

  public static DataHolder getInstance() {
if (dataHolder == null)
{
    dataHolder = new DataHolder();
}
return dataHolder;
 }


  private  String item;

 public String getItem() {
return item;
 }

public void setItem(String item) {
 this.item = item;
     }

 }

--------------------------Categories_Activity--------------------------------- --------------------------Categories_Activity------------------------ ----------

This Activity once it's being called a listview will show allowing user to select an item.此活动一旦被称为列表视图,将显示允许用户选择一个项目。 So once an Item has been selected it will start the MainActivity in order to refresh the Text field in the search Fragment因此,一旦选择了一个项目,它将启动 MainActivity 以刷新搜索片段中的文本字段

 public class Categories_Activity extends AppCompatActivity implements View.OnClickListener {

ImageView BacktoMainCate;
ListView subCate;
public String selectedItem;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_subcategory);
    subCate = (ListView)findViewById(R.id.listview_subcate);
    BacktoMainCate = (ImageView)findViewById(R.id.BacktoMainCate);
    BacktoMainCate.setOnClickListener(this);

           final SearchFragment SF = new SearchFragment();
    subCate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             selectedItem = String.valueOf(parent.getItemAtPosition(position));
            DataHolder.getInstance().setItem(selectedItem);
            SF.ChangeStatus(true);

  Intent in = new Intent(Categories_Activity.this,SuccessActivity.class);
                startActivity(in);

    });

}





@Override
public void onClick(View v) {

    if (v == BacktoMainCate){
        //startActivity(new Intent(this,AllCateActivity.class));
        finish();
    }




   }
}

I have used SharedPreferences on both Fragment/Activity life cycle.我在 Fragment/Activity 生命周期中都使用了 SharedPreferences。

So once the Categories_Activity get started, Main Activity will go in onPause and with it all its Fragments will be put in onPause.因此,一旦 Categories_Activity 启动,Main Activity 将进入 onPause,并且它的所有 Fragment 将被置于 onPause。 When user clicks on one of the ListView items in the Categories_Activity, data of that selected item will be store inside SharedPreferences like:当用户单击 Categories_Activity 中的 ListView 项目之一时,所选项目的数据将存储在 SharedPreferences 中,例如:

PreferenceManager.getDefaultSharedPreferences(Categories_Activity .this)
        .edit().putString(key, selectedItemInfo).apply();

Then override onResume() method inside MainActivity and SearchFragment:然后在 MainActivity 和 SearchFragment 中覆盖 onResume() 方法:

@Override
public void onResume() {
super.onResume();
String value = PreferenceManager.getDefaultSharedPreferences(getContext())
        .getString(key, "");
if (value != null && !value.isEmpty()) {
    Categories.setText(value);
  PreferenceManager.getDefaultSharedPreferences(getContext()).edit().remove(key);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM