简体   繁体   中英

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.

Here is what I have now:

Thats the function that sorting my adapter:

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

LightsFragment - handling the adapter that should be sorted.

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.

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. I already have a function that sorting adapter. Just don't know how to access the adapter from the Main activity.

Store the Fragment instance in Activity and access it any time.

 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

First, create a getter method for your LightsAdapter in Fragment. After, get the fragment in the mainActivity with the help of

        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 .

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.

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
        }

     }
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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