简体   繁体   English

如何从另一个片段中对适配器进行排序

[英]How to sort adapter from another fragment

I have RecyclerView on LightsFragment , and I want to sort it from the MainActivity which contain this fragment.我在LightsFragment上有RecyclerView ,我想从包含此片段的 MainActivity 中对其进行排序。

Here is what I have now:这是我现在拥有的:

Thats the function that sorting my adapter:那就是对我的适配器进行排序的 function:

Collections.sort(lights);
lightsAdapter.updateData(lights);

LightsFragment - handling the adapter that should be sorted. LightsFragment - 处理应该排序的适配器。

public class LightsFragment extends Fragment implements LightsPresenter, View.OnClickListener {

    private RecyclerView RVLights;
    private ArrayList<Light> lights = new ArrayList<>();
    private Light light;
    private LightsAdapter lightsAdapter;
    private LightsView presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lights, container, false);
        presenter = new LightsView(lights, light, this);
        RVLights = view.findViewById(R.id.RVLights);
        presenter.loadData();
        return view;
    }

    @Override
    public void setAdapter() {
        lightsAdapter = new LightsAdapter(getActivity(), lights);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        RVLights.setLayoutManager(layoutManager);
        RVLights.setAdapter(lightsAdapter);
    }
}

MainActivity - handling 3 buttons, one of the buttons should sort adapter. MainActivity - 处理 3 个按钮,其中一个按钮应排序适配器。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Greetings mGreetings = new Greetings();
    private TextView mTVgreetings;
    private Button BTNmLights, BTNmGarage, BTNmSortBy;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mTVgreetings = findViewById(R.id.TVgreetings);
        initFunctions();
    }

    public void showLightsFragment() {
        getSupportFragmentManager().beginTransaction().add(R.id.main_fragment_container, new LightsFragment())
                .commit();
    }

    public void initFunctions() {
        showLightsFragment();
        showGreetings();
    }

    public void showGreetings() {
        mTVgreetings.setText(mGreetings.getGreetings());
    }

    public void initView() {
        BTNmLights = findViewById(R.id.BTNlights);
        BTNmGarage = findViewById(R.id.BTNgarage);
        BTNmSortBy = findViewById(R.id.BTNsort);
        BTNmLights.setOnClickListener(this);
        BTNmGarage.setOnClickListener(this);
        BTNmSortBy.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.BTNlights:
                //TODO open lights fragment...
                break;
            case R.id.BTNgarage:
                //TODO open garage fragment...
                break;
            case R.id.BTNsort:
                //TODO make drop down with sort light and garage.
                break;
        }
    }
}

I want that when i pressing on sort button on main activity, the adapter from LightsFragment will be sorted.我希望当我按下主要活动上的排序按钮时,LightsFragment 的适配器将被排序。 I already have a function that sorting adapter.我已经有一个 function 那个排序适配器。 Just don't know how to access the adapter from the Main activity.只是不知道如何从 Main 活动访问适配器。

Store the Fragment instance in Activity and access it any time.将 Fragment 实例存储在 Activity 中,随时访问。

 private LightsFragment lightsFragment= new LightsFragment();

 public void showLightsFragment() { 
   getSupportFragmentManager()
     .beginTransaction().add(R.id.main_fragment_container, this.lightFragment)
                    .commit();
 }


@Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.BTNlights:
                //TODO open lights fragment...
                break;
            case R.id.BTNgarage:
                //TODO open garage fragment...
                break;
            case R.id.BTNsort:
                this.lightFragment.sortLights()
                //TODO make drop down with sort light and garage.
                break;
        }
    }

and in your LightFragment you should have a method to find the RecylerView and get it's adapter to update the list and notifyDataSetChanged在你的 LightFragment 中你应该有一个方法来找到 RecylerView 并获取它的适配器来更新列表和notifyDataSetChanged

First, create a getter method for your LightsAdapter in Fragment.首先,在 Fragment 中为您的 LightsAdapter 创建一个 getter 方法。 After, get the fragment in the mainActivity with the help of之后,在 mainActivity 的帮助下获取片段

        Fragment yourFragment=getSupportFragmentManager().findFragmentById(R.id.main_fragment_container);

Later, check if the fragment is your fragment or not:稍后,检查片段是否是您的片段:

if(yourFragment instanceOf LightsFragment)
{
   yourFragment.getAdapter();
} 

You can do that using interface .您可以使用interface来做到这一点。

create an interface like创建一个类似的界面

public interface UpdateFrag {
     void sortAdapter(int sortType);
}

In your Activity do the following在您的活动中执行以下操作

 UpdateFrag updatFrag ;// add this line

  public void showLightsFragment() {
        Fragment lightsFragment = new LightsFragment();
        updatFrag = lightsFragment; // here you initialize updatFrag

        fragment.getSupportFragmentManager().beginTransaction().add(R.id.main_fragment_container, lightsFragment)
                .commit();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.BTNlights:

                break;
            case R.id.BTNgarage:
                //TODO open garage fragment...
                break;
            case R.id.BTNsort:
                // here you should call sortAdapter method.
                updatFrag.sortAdapter(1) // suppose 1 for light and 2 for garage
                break;
        }
    }

Now in your LightsFragment implement the interface.现在在您的LightsFragment中实现接口。

public class LightsFragment extends Fragment implements LightsPresenter, View.OnClickListener, UpdateFrag {
    // ....

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

    @Override
    public void setAdapter() {
       // ....
    }

     @Override
     public void sortAdapter(int sortType) {
        if (sortType == 1){
          // here you can sort your adapter according to light
        }else{
          // here you can sort your adapter according to garage
        }

     }
}


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

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