Android - 在1个Activity + XML中有2个RecyclerViews是否可行?

时间:2017-09-01 13:40:05

标签: android android-recyclerview onclicklistener

有没有办法在1个XML +活动中拥有2个回收站视图?

当我实现我的适配器的ListItemClickListener时,我遇到了一些麻烦,因为它说'Class HomeActivity必须在'ListItemClickListener'中声明为abstract或实现抽象方法'onListItemClick(MyIssueDao)'。

是否可以与我的2个回收站视图共享方法'onListItemClick'

HomeActivity.java

public class HomeActivity extends AppCompatActivity implements 
IssuesAdapter.ListItemClickListener, MyIssuesAdapter.ListItemClickListener {

private Context context = HomeActivity.this;
public static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static RecyclerView myIssuesRecyclerView;
private String currentParent = "";
static View.OnClickListener issueOnClickListener;
static View.OnClickListener myIssueOnClickListener;

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

    // my issues
    myIssuesRecyclerView = (RecyclerView) findViewById(R.id.my_issues_list_recycler_view);
    myIssuesRecyclerView.setHasFixedSize(true);
    myIssuesRecyclerView.setItemAnimator(new DefaultItemAnimator());
    layoutManager = new LinearLayoutManager(this);
    myIssuesRecyclerView.setLayoutManager(layoutManager);


    // issues types
    recyclerView = (RecyclerView) findViewById(R.id.issues_list_recycler_view);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    // TODO - Rest method to receive level types, initially we ask for the first level
    ArrayList<IssueDao> issuesList = new ArrayList<>();
    ArrayList<String> subtypes = new ArrayList<>();
    subtypes.add("Sucursales");
    subtypes.add("ATM");
    IssueDao issue = new IssueDao("0", "Instalaciones", subtypes, "setup", "");
    issuesList.add(issue);
    subtypes = new ArrayList<>();
    subtypes.add("Internet");
    subtypes.add("Móvil");
    subtypes.add("Contact Center");
    issue = new IssueDao("1", "Canales", subtypes, "channels", "");
    issuesList.add(issue);
    subtypes = new ArrayList<>();
    subtypes.add("Ideas y sugerencias");
    issue = new IssueDao("2", "Sugerencias", subtypes, "suggestions", "");
    issuesList.add(issue);

    adapter = new IssuesAdapter(issuesList, HomeActivity.this);

    recyclerView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    MenuItem saveItem = menu.findItem(R.id.action_save);
    saveItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    saveItem.setVisible(false);
    return true;
}


@Override
public void onListItemClick(IssueDao issueDao) {
    currentParent = issueDao.getIdParent();
    // TODO - Rest method to receive level types, we ask for the sublevels of the selected issue.
    ArrayList<IssueDao> issuesList = new ArrayList<>();
    ArrayList<String> subtypes = new ArrayList<>();
    subtypes.add("Nacionales");
    subtypes.add("Internacionales");
    IssueDao issue = new IssueDao("3", "Sucursales", subtypes, "setup", "0");
    issuesList.add(issue);
    subtypes = new ArrayList<>();
    subtypes.add("Nacionales");
    subtypes.add("Internacionales");
    issue = new IssueDao("4", "ATM", subtypes, "channels", "0");
    issuesList.add(issue);
    adapter = new IssuesAdapter(issuesList, HomeActivity.this);

    recyclerView.setAdapter(adapter);
    // TODO - If it has no sublevels we show the screen to fill the info of the issue
    //  TODO - delete this
    subtypes = new ArrayList<>();
    if(subtypes.isEmpty()){
        Intent newIssue = new Intent(context, NewIssueActivity.class);
        newIssue.putExtra(IntentEnum.IDTYPEISSUE.getCode(), issueDao.getId());
        startActivity(newIssue);
    }
}

}

IssuesAdapter.java

public class IssuesAdapter extends RecyclerView.Adapter<IssuesAdapter.IssuesHolder> {
    private static ListItemClickListener mOnClickListener;
    ArrayList<IssueDao> listIssues;

    public IssuesAdapter(ArrayList<IssueDao> listIssues, ListItemClickListener listener) {
    this.listIssues = listIssues;
    mOnClickListener = listener;
}

public interface ListItemClickListener {
    void onListItemClick(IssueDao issueItem);
}

public class IssuesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView textNameIssue;
    TextView textSubtypes;
    ImageView imageTypeIssue;
    Context context;

    public IssuesHolder(View itemView) {
        super(itemView);
        this.textNameIssue = (TextView) itemView.findViewById(R.id.textNameIssue);
        this.textSubtypes = (TextView) itemView.findViewById(R.id.textSubtypes);
        this.imageTypeIssue = (ImageView) itemView.findViewById(R.id.imageTypeIssue);
        this.context = itemView.getContext();
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        int clickedPosition = getAdapterPosition();
        IssueDao assetItem = getItem(clickedPosition);
        mOnClickListener.onListItemClick(assetItem);
    }
}

@Override
public IssuesHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.elem_type_issue, parent, false);

    view.setOnClickListener(issueOnClickListener);

    IssuesHolder myViewHolder = new IssuesHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(final IssuesHolder holder, final int listPosition) {
    TextView textNameIssue = holder.textNameIssue;
    TextView textSubtypes = holder.textSubtypes;
    ImageView imageTypeIssue = holder.imageTypeIssue;

    textNameIssue.setText(listIssues.get(listPosition).getName().toUpperCase());
    textSubtypes.setText(android.text.TextUtils.join(" | ", listIssues.get(listPosition).getSubtypes()).toUpperCase());

    String type = listIssues.get(listPosition).getType();
    switch (type){
        case "setup":
            imageTypeIssue.setImageResource(R.drawable.ic_home);
            break;
        case "channels":
            imageTypeIssue.setImageResource(R.drawable.ic_phone);
            break;
        case "suggestions":
            imageTypeIssue.setImageResource(R.drawable.ic_lightbulb);
            break;
        default:
            imageTypeIssue.setImageResource(R.drawable.ic_launcher);
            break;
    }
}

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

public IssueDao getItem(int position) {
    return listIssues.get(position);
}

}

MyIssuesAdapter.java

public class MyIssuesAdapter extends RecyclerView.Adapter<MyIssuesAdapter.MyIssuesHolder> {
    private static ListItemClickListener mOnClickListener;
    ArrayList<MyIssueDao> listIssues;

    public MyIssuesAdapter(ArrayList<MyIssueDao> listIssues, ListItemClickListener listener) {
        this.listIssues = listIssues;
        mOnClickListener = listener;
    }

    public interface ListItemClickListener {
        void onListItemClick(MyIssueDao issueItem);
    }

    public class MyIssuesHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView textNameIssue;
        TextView myIssueDate;
        ImageView imageTypeIssue;
        ImageView circleCalendarImage;
        Context context;

        public MyIssuesHolder(View itemView) {
            super(itemView);
/*            this.textNameIssue = (TextView) itemView.findViewById(R.id.textNameIssue);
            this.imageTypeIssue = (ImageView) itemView.findViewById(R.id.imageTypeIssue);*/
            this.circleCalendarImage = (ImageView) itemView.findViewById(R.id.circleCalendarImage);
            this.myIssueDate = (TextView) itemView.findViewById(R.id.parent_list_issue_title_text_view);
            this.context = itemView.getContext();
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view){
            int clickedPosition = getAdapterPosition();
            MyIssueDao assetItem = getItem(clickedPosition);
            mOnClickListener.onListItemClick(assetItem);
        }
    }

    @Override
    public MyIssuesHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_issue_header, parent, false);

        view.setOnClickListener(issueOnClickListener);

        MyIssuesHolder myViewHolder = new MyIssuesHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyIssuesHolder holder, final int listPosition) {
        TextView textNameIssue = holder.textNameIssue;
        ImageView imageTypeIssue = holder.imageTypeIssue;

        textNameIssue.setText(listIssues.get(listPosition).getName().toUpperCase());

/*        String type = listIssues.get(listPosition).getType();
        switch (type){
            case "setup":
                imageTypeIssue.setImageResource(R.drawable.ic_home);
                break;
            case "channels":
                imageTypeIssue.setImageResource(R.drawable.ic_phone);
                break;
            case "suggestions":
                imageTypeIssue.setImageResource(R.drawable.ic_lightbulb);
                break;
            default:
                imageTypeIssue.setImageResource(R.drawable.ic_launcher);
                break;
        }*/
    }

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

    public MyIssueDao getItem(int position) {
        return listIssues.get(position);
    }

}

IssueDao.java

import java.util.ArrayList;

public class IssueDao {
    private String id;
    private String idParent;
    private String name;
    private String type;
    private ArrayList<String> subtypes;

    public IssueDao(){}

    public IssueDao(String id, String name, ArrayList<String> subtypes, String type, String idParent) {
        this.id=id;
        this.idParent=idParent;
        this.name = name;
        this.type=type;
        this.subtypes=subtypes;
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIdParent() {
        return idParent;
    }

    public void setIdParent(String idParent) {
        this.idParent = idParent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<String> getSubtypes() {
        return subtypes;
    }

    public void setSubtypes(ArrayList<String> subtypes) {
        this.subtypes = subtypes;
    }
}

MyIssueDao.java

import java.util.ArrayList;

public class MyIssueDao {

    private String id;
    private String name;
    private String type;

    public MyIssueDao(){}

    public MyIssueDao(String id, String name, String type) {
        this.id=id;
        this.name=name;
        this.type=type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

activity_home.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutHome"
tools:context=".HomeActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/barMenuHome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:id="@+id/action_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|top"
                android:src="@drawable/icono_notificacion" />

            <TextView
                android:id="@+id/tv_toolbar_title"
                style="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/_20dp"
                android:text="@string/app_name" />

            <ImageView
                android:id="@+id/action_help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|top"
                android:layout_marginRight="@dimen/_5dp"
                android:alpha="0.75"
                android:paddingLeft="@dimen/_10dp"
                android:src="@drawable/ic_help" />

            <ImageView
                android:id="@+id/action_trophy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|top"
                android:src="@drawable/ic_trophy" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:id="@+id/yourIssuesLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/barMenuHome"
        android:orientation="vertical">

        <TextView
            android:id="@+id/your_text"
            style="?android:attr/textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="@dimen/_10dp"
            android:layout_marginTop="@dimen/_10dp"
            android:gravity="center"
            android:text="@string/your_issues"
            android:textColor="@color/customGrey2" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_issues_list_recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="170dp"
            android:layout_below="@id/your_text"
            android:scrollbars="vertical" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/yourIssuesLayout"
        android:paddingTop="@dimen/_15dp"
        android:background="@drawable/gradient_background"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/issues_list_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/gradient_background"
            android:scrollbars="vertical" />

    </RelativeLayout>

</RelativeLayout>

list_item_issue_header.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">

<ImageView
    android:id="@+id/circleCalendarImage"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginLeft="@dimen/_5dp"
    android:layout_marginTop="8dp"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    android:src="@drawable/circle_calendar_shape"/>

<TextView
    android:id="@+id/parent_list_issue_title_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginRight="3dp"
    android:text="FRIDAY - JANUARY 29TH, 2016"
    android:textSize="12sp"
    android:textColor="@color/colorGrayDark"
    android:textStyle="bold"
    android:layout_centerVertical="true"
    android:layout_toEndOf="@+id/circleCalendarImage"
    android:layout_marginStart="12dp" />

谢谢!

2 个答案:

答案 0 :(得分:0)

您只在 HomeActivity.java 中实现了两个接口中的一个:一个在 IssueDao.java 中为onListItemClick(IssueDao),另一个在{{1在 MyIssueDao.java

此处,您仅实施了onListItemClick(MyIssueDao)。实施另一个,它应该工作。

答案 1 :(得分:0)

以下是答案:

您需要将Thread Started Before Thread Ended Thread Ended After Thread Ended .xml放入LinearLayout并使用ScrollView作为wrap_content RecyclerView's

layout_height

此外, <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="@dimen/list_header" android:background="@color/header" android:gravity="center" android:text="@string/find_friends__already_playing" android:visibility="visible" /> <android.support.v7.widget.RecyclerView android:id="@+id/in_app_friends" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:background="@color/white"/> <TextView android:layout_width="match_parent" android:layout_height="@dimen/list_header" android:background="@color/find_friends__header" android:gravity="center" android:text="@string/find_friends__invite_friends" /> <android.support.v7.widget.RecyclerView android:id="@+id/friends_to_invite" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"/> </LinearLayout> </ScrollView> RecyclerView存在错误,因此您必须使用自定义布局管理器。 看看这篇文章:[如何让WRAP_CONTENT在RecyclerView上工作] [1]

在您的java代码中,设置第二个wrap_content 它对我有用!!!