如何在PlaceholderView中将OnClickListener设置为Card?

时间:2017-11-13 14:11:45

标签: android onclicklistener

我正在使用PlaceHolderView库来显示Facebook Android应用程序等无限内容列表。我正在使用InfinitePlaceHolderView。它看起来像这样:

以下是output

的样子

现在如果故事很大,那么用户可以点击卡片视图,它将带他进入一个新的活动,在那里会有一个故事的细节。为此,我需要添加一个Listener。但我根本找不到任何文档,如何实现它。文档显示所有这些卡都是动态创建的,并且这些卡中没有任何一个具有任何ID,因此我可以放置一个监听器。

如何实施?

2 个答案:

答案 0 :(得分:0)

在文档的第三步中,您有一个事件绑定示例:

@Animate(Animation.ENTER_LEFT_DESC)
@NonReusable
@Layout(R.layout.gallery_item_big)
public class ImageTypeBig {

    @View(R.id.imageView)
    private ImageView imageView;

    private String mUrl;
    private Context mContext;
    private PlaceHolderView mPlaceHolderView;

    public ImageTypeBig(Context context, PlaceHolderView placeHolderView, String url) {
        mContext = context;
        mPlaceHolderView = placeHolderView;
        mUrl = url;
    }

    @Resolve
    private void onResolved() {
        Glide.with(mContext).load(mUrl).into(imageView);
    }

    @LongClick(R.id.imageView)
    private void onLongClick(){
        mPlaceHolderView.removeView(this);
    }

}

更多信息: https://github.com/janishar/PlaceHolderView#step-3-create-item-class-to-bind-and-define-view-operations

答案 1 :(得分:0)

所以,我的所作所为如下:

在包含上述视图的feed.xml文件中,我为整个视图设置了id。然后我使用了id并使用here中的ItemView.java

feed.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_100"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.example.shamsad.tutionhub.activities.FeedActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:id="@+id/tbHome"
        android:background="@color/colorPrimary"
        app:titleTextColor="@color/grey_100">
    </android.support.v7.widget.Toolbar>

    <com.mindorks.placeholderview.InfinitePlaceHolderView
        android:id="@+id/loadMoreView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

ItemView.java

@Click(R.id.rootView)
    public void onCardClick() {
        Intent intent = new Intent(mContext, DetailActivity.class);
        intent.putExtra("title", mInfo.getTitle());
        intent.putExtra("caption", mInfo.getCaption());
        intent.putExtra("salary", mInfo.getSalary());
        intent.putExtra("routine", mInfo.getRoutine());
        intent.putExtra("time", mInfo.getTime());
        Log.d("sajid", "onClick");
        mContext.startActivity(intent);
    }

因此,在此onCardClick()方法中,可点击的ID由此代码@Click(R.id.rootView)设置。这就是全部。它基本上将整个card-view设置为可点击的东西。

还要感谢这个伟大图书馆的作者 Janisher Ali 。我联系了他,他帮了很多忙,而且很有效。

This是一个很棒的图书馆。对我来说比RecyclerView要好得多。你可以尝试一下:))