卧式recyclerView的内部项目在立式recyclerView中

时间:2018-09-09 22:15:28

标签: android android-recyclerview asynctaskloader

我的应用程序中有一个问题...我正在使用ramotion.foldingcell库,并使用它通过recyclerView创建列表,当在列表的单元格中轻按时,它会打开另一个recyclerView,但在水平方向...我从json获取数据和在AsynctaskLoader中使用 加载数据....我的问题是,当onbindingView中的水平recyclerView获取所有数据列表时,垂直列表中的每个项目都不在特定行中....每行应该有7个项目与其他行不在同一行

image from activity

自定义垂直列表的代码:

package com.example.m_elblasy.psedv2.list;

import android.graphics.Bitmap;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import com.example.m_elblasy.psedv2.R;
import com.example.m_elblasy.psedv2.activities.Projects;
import com.example.m_elblasy.psedv2.adapters.ListOFProjects;

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

public class ItemsForCells {

    private Bitmap image;
    private ArrayList<ProjectsList> listOFProjects = new ArrayList<>();

    public ItemsForCells(){

    }

    public ArrayList<ProjectsList> getListOFProjects() {
        return listOFProjects;
    }

    public void setListOFProjects(ArrayList<ProjectsList> listOFProjects) {
        this.listOFProjects = listOFProjects;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }
}

自定义水平列表代码:

 package com.example.m_elblasy.psedv2.list;

import android.graphics.Bitmap;

import com.example.m_elblasy.psedv2.R;

import java.util.ArrayList;

public class ProjectsList  {

    Bitmap imageOfProject;
    String dis;

    public ProjectsList(Bitmap imageOfProject, String dis) {
        this.imageOfProject = imageOfProject;
        this.dis = dis;
    }

    public Bitmap getImageOfProject() {
        return imageOfProject;
    }

    public String getDis() {
        return dis;
    }
}

卧式recyclerView的代码:

        package com.example.m_elblasy.psedv2.adapters;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.RequestOptions;
    import com.example.m_elblasy.psedv2.R;
    import com.example.m_elblasy.psedv2.activities.Projects;
    import com.example.m_elblasy.psedv2.list.ItemsForCells;
    import com.example.m_elblasy.psedv2.list.ProjectsList;

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

    public class ListOFProjects extends RecyclerView.Adapter<ListOFProjects.MyViewHolder> {
        private static final String TAG = "ListOFProjects";
        private ArrayList<ProjectsList> projectsLists;
        private Context context;



        public ListOFProjects(Context context, ArrayList<ProjectsList> projectsLists) {
            this.projectsLists = projectsLists;
            this.context = context;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.items_projects, parent, false);
            final MyViewHolder myViewHolder = new MyViewHolder(view);

            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            final ProjectsList list = projectsLists.get(position);

            holder.disOfProject.setText(list.getDis());
            Bitmap bitmap = list.getImageOfProject();

            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(R.drawable.shape);

            Glide.with(context)
                    .asBitmap()
                    .load(bitmap)
                    .apply(requestOptions.centerCrop().override(500, 450))
                    .into(holder.imageOfProject);

            Log.i(TAG, "onBindViewHolder: " + projectsLists.size());


        }

        @Override
        public int getItemCount() {
            return projectsLists.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            private ImageView imageOfProject;
            private TextView disOfProject;

            public MyViewHolder(View itemView) {
                super(itemView);
                imageOfProject = (ImageView) itemView.findViewById(R.id.image_projects_list);
                disOfProject = (TextView) itemView.findViewById(R.id.nameofproject);
            }
        }

    }

病毒性recyclerView的代码:

package com.example.m_elblasy.psedv2.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.m_elblasy.psedv2.CustomItemClickListener;
import com.example.m_elblasy.psedv2.CustomLinearLayoutManager;
import com.example.m_elblasy.psedv2.R;
import com.example.m_elblasy.psedv2.activities.Projects;
import com.example.m_elblasy.psedv2.list.ItemsForCells;
import com.example.m_elblasy.psedv2.list.ProjectsList;
import com.ramotion.foldingcell.FoldingCell;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class CellsProjectsAdapter extends RecyclerView.Adapter<CellsProjectsAdapter.ViewHolder> {
    private static final String TAG = "CellsProjectsAdapter";
    private Context context;
    private List<ItemsForCells> itemsForCells;

    public CellsProjectsAdapter(Context context, List<ItemsForCells> itemsForCells) {
        this.context = context;
        this.itemsForCells = itemsForCells;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cells, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final ItemsForCells items = itemsForCells.get(position);

        Log.i(TAG, "onBindViewHolder: " + itemsForCells.size());
        try {
            holder.titleImageView.setImageBitmap(items.getImage());
            //ArrayList<ProjectsList> projects = items.getListOFProjects();
            ListOFProjects projectsList = new ListOFProjects(context, items.getListOFProjects());
            RecyclerView.LayoutManager layoutManager =
                    new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            holder.recyclerView.setLayoutManager(layoutManager);
            holder.recyclerView.setAdapter(projectsList);
            projectsList.notifyDataSetChanged();
            holder.recyclerView.setNestedScrollingEnabled(false);

            holder.foldingCell.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.foldingCell.toggle(false);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return itemsForCells.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView titleImageView;
        FoldingCell foldingCell;
        RecyclerView recyclerView;

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

            titleImageView = (ImageView) itemView.findViewById(R.id.title_image_cell);
            foldingCell = (FoldingCell) itemView.findViewById(R.id.folding_cell);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.list_item_cell);

        }
    }
}

显示此内容的活动:

package com.example.m_elblasy.psedv2.activities;

import android.app.LoaderManager;
import android.content.ClipData;
import android.content.Context;
import android.content.Loader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.m_elblasy.psedv2.CustomItemClickListener;
import com.example.m_elblasy.psedv2.R;
import com.example.m_elblasy.psedv2.adapters.CellsProjectsAdapter;
import com.example.m_elblasy.psedv2.adapters.ListOFProjects;
import com.example.m_elblasy.psedv2.list.ItemsForCells;
import com.example.m_elblasy.psedv2.list.ProjectsList;
import com.example.m_elblasy.psedv2.loaders.ProjectsLoader;
import com.ramotion.foldingcell.FoldingCell;

import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Projects extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<ItemsForCells>> {
    private static final String TAG = "Projects";
    //private static final String JSON_PROJECTS_URL = "https://api.myjson.com/bins/69w78";
    private static final String JSON_PROJECTS_URL = "https://api.myjson.com/bins/pc04g";
    static CellsProjectsAdapter cellsProjectsAdapter;
    RecyclerView mRecyclerView;
    ArrayList<ItemsForCells> data;
    private static final int PROJECTS_LOADER_ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projects);

        data = new ArrayList<>();

        mRecyclerView = findViewById(R.id.list_item_projects);


        cellsProjectsAdapter = new CellsProjectsAdapter(this, data);
        RecyclerView.LayoutManager mLayoutManager =
                new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);


        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {

            getLoaderManager().initLoader(PROJECTS_LOADER_ID, null, this);
        }

    }


    @Override
    public Loader<List<ItemsForCells>> onCreateLoader(int i, Bundle bundle) {
        return new ProjectsLoader(this, JSON_PROJECTS_URL);
    }

    @Override
    public void onLoadFinished(Loader<List<ItemsForCells>> loader, List<ItemsForCells> itemsForCells) {

        if (itemsForCells != null && !itemsForCells.isEmpty()) {
            data.clear();
            data.addAll(itemsForCells);
            mRecyclerView.setAdapter(cellsProjectsAdapter);
            cellsProjectsAdapter.notifyDataSetChanged();
            }
    }

    @Override
    public void onLoaderReset(Loader<List<ItemsForCells>> loader) {
        loader.forceLoad();

    }

}

0 个答案:

没有答案