我正在尝试在 recycleview 的 gridlayout 上存档自动设置的列大小。我找到了它的代码,但我是 android 的新手,所以我现在不知道如何在我的片段类中实现它。请指导我如何实施
1) 基础片段类
public class BaseFragment extends android.app.Fragment implements RecycleAdapter.GetDataFromAdapter {
@Bind(R.id.recyclerView) RecyclerView recyclerView;
private static final String STATE_MOVIES = "state_movies";
private View rootView = null;
private ServiceManager serviceManager;
private RecycleAdapter recyclerAdapter;
private MovieResponse movieResponse;
private List<Results> resultsList;
private Results results;
private OnFragmentInteractionListener mListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(STATE_MOVIES, (ArrayList<? extends Parcelable>) resultsList);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.High_rated:
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
makeService("TOP");
return true;
}
case R.id.most_Popular:
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
makeService("POPULAR");
return true;
}
case R.id.favoriteList:
if(item.isChecked()){
item.setChecked(false);
}else{
item.setChecked(true);
Intent intent=new Intent(getActivity(), FavoriteActivity.class);
startActivity(intent);
return true;
}
}
return super.onOptionsItemSelected(item);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_base, container, false);
ButterKnife.bind(this,rootView);
resultsList=new ArrayList<Results>();
configRecycleView();
if (savedInstanceState==null){
makeService("POPULAR");
}else{
resultsList=savedInstanceState.getParcelableArrayList(STATE_MOVIES);
recyclerAdapter.addMovieList(resultsList);
}
return rootView;
}
private void configRecycleView() {
recyclerView.setHasFixedSize(true);
recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),
2));
recyclerAdapter = new RecycleAdapter(getActivity(), BaseFragment.this
);
recyclerView.setAdapter(recyclerAdapter);
}
private void makeService(String query){
serviceManager = new ServiceManager();
Call<MovieResponse> listCall;
if (query=="TOP") {
listCall= serviceManager.getJSONData().getTopMovies();
}else{
listCall= serviceManager.getJSONData().getPopMovies();
}
listCall.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
if (response.isSuccessful()) {
Results[] results = response.body().getResults();
resultsList = new ArrayList<Results>(Arrays.asList(results));
recyclerAdapter.addMovieList(resultsList);
Toast.makeText(getActivity(), "Data Fatch", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCurrentMovie(Results currentMovie) {
mListener.onFragmentInteraction(currentMovie);
}
@Override
public void onAttach(Activity context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Results result);
}
}
2)recycleview中gridlayout上autosetcolumn的方法
public class VarColumnGridLayoutManager extends GridLayoutManager {
private int minItemWidth;
public VarColumnGridLayoutManager(Context context, int minItemWidth) {
super(context, 1);
this.minItemWidth = minItemWidth;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler,
RecyclerView.State state) {
updateSpanCount();
super.onLayoutChildren(recycler, state);
}
private void updateSpanCount() {
int spanCount = getWidth() / minItemWidth;
if (spanCount < 1) {
spanCount = 1;
}
this.setSpanCount(spanCount);
}}