recycleler视图GridLayoutManager的第一行比其他行少一个

时间:2016-11-06 11:26:52

标签: android gridlayoutmanager

我有一个带有GridLayoutManager的recyclerview,它有3个cols,但在第一行只显示了2个这样的项目:

enter image description here

我在这段代码中出错:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.florent37.materialviewpager.MaterialViewPagerHelper;
import com.github.florent37.materialviewpager.adapter.RecyclerViewMaterialAdapter;

import java.util.ArrayList;
import java.util.List;


public class Main_Recycle6 extends Fragment {

    private RecyclerView recyclerView;
    private Main_Recycle_Adapter2 movieRecyclerviewAdapter;

    private List<Main> movies;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.main_grid4, container, false);


    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        movies = new ArrayList<>();

        recyclerView = (RecyclerView) getView().findViewById(R.id.Recycle1);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
       // recyclerView.setNestedScrollingEnabled(false);
        RecyclerView.LayoutManager layoutManager;
        layoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(layoutManager);


        movies = getData();

        movieRecyclerviewAdapter = new Main_Recycle_Adapter2 (getActivity(), movies);
        recyclerView.setAdapter(movieRecyclerviewAdapter);




        RecyclerViewMaterialAdapter movieRecyclerviewAdapterMaterial = new RecyclerViewMaterialAdapter(movieRecyclerviewAdapter);

        // 4. set adapter
        recyclerView.setAdapter(movieRecyclerviewAdapterMaterial);

        //5. Register the recyclerview to MaterialViewPager
        MaterialViewPagerHelper.registerRecyclerView(getActivity(), recyclerView, null);

    }


    public static Main_Recycle newInstance() {
        return new Main_Recycle();
    }

    private List<Main> getData() {
        List<Main> movies = new ArrayList<>();

        String names[] = {                    "آزمون 1",
                "آزمون 2",
                .....


        };
        String logos[] = {"i1",
                "i2",
                .....

        };

        for (int i = 0; i < names.length; i++) {
            Main newMovie = new Main(names[i], logos[i]);

            movies.add(newMovie);
        }

        return movies;
    }

适配器:

    import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

public class Main_Recycle_Adapter2 extends RecyclerView.Adapter<Main_Recycle_Adapter2.MovieViewHolder> {
    List<Main> movies = Collections.emptyList();
    LayoutInflater layoutInflater;
    Context context;

    public Main_Recycle_Adapter2(Context context, List<Main> movies){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.movies = movies;


    }
    //;;;;;;;;;;;;;;
    @Override
    public int getItemCount() {
        return movies.size();
    }
    @Override
    public long getItemId(int position) {
        return position;

    }


    @Override
    public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType )
    {
        View row;

            row = layoutInflater.inflate(R.layout.gridview_custom_layout, parent, false);



        MovieViewHolder holder = new MovieViewHolder(row);

        return holder;
    }


    @Override
    public void onBindViewHolder(MovieViewHolder holder, int position) {
        Main thisMovie = movies.get(position);


        holder.tozih.setText(thisMovie.getName());
        holder.tablooo.setImageResource(context.getResources().getIdentifier(thisMovie.getLogo(), "drawable", context.getPackageName()));
    }

    public class MovieViewHolder extends RecyclerView.ViewHolder {

        private TextView tozih;

        private ImageView tablooo;

        public MovieViewHolder(View itemView) {
            super(itemView);

            tozih = (TextView) itemView.findViewById(R.id.gridview_text);

            tablooo = (ImageView) itemView.findViewById(R.id.gridview_image);
        }
    }
}

这是网格代码 recyclerview在一个片段中,片段在另一个活动中

RecyclerView.LayoutManager layoutManager;
            layoutManager = new GridLayoutManager(getActivity(), 3);
            recyclerView.setLayoutManager(layoutManager);

提前致谢

0 个答案:

没有答案