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

时间:2019-11-07 10:20:41

标签: java android android-recyclerview

我在RecyclerView上有LightsFragment,我想从包含该片段的MainActivity中对其进行排序。

这是我现在拥有的:

多数民众赞成在排序我的适配器的功能:

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

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

我希望当我在主要活动上按排序按钮时, 来自LightsFragment的适配器将被排序。 我已经有了排序适配器的功能。 只是不知道如何从Main活动中访问适配器。

3 个答案:

答案 0 :(得分:1)

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

在LightFragment中,您应该有一个方法可以找到RecylerView并获得它的适配器来更新列表和notifyDataSetChanged

答案 1 :(得分:0)

首先,在Fragment中为LightsAdapter创建一个getter方法。 之后,借助

在mainActivity中获取该片段
        Fragment yourFragment=getSupportFragmentManager().findFragmentById(R.id.main_fragment_container);

以后,请检查片段是否为您的片段:

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

答案 2 :(得分:0)

您可以使用interface来做到这一点。

创建类似

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

在“活动”中执行以下操作

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

现在在您的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
        }

     }
}