RecyclerView onItemClicked回调

时间:2015-05-27 16:05:09

标签: android onclick onclicklistener android-recyclerview

我正在使用this教程实现带有RecyclerView的导航抽屉,但我不想应用整个教程,我只想处理用户的点击。我已在onClickListener课程的ViewHolder内实施了MyAdapter

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
    int Holderid; ImageView imageView, profile;
    TextView textView, Name, email, drawerTitle;

    private ClickListener listener;
    // ViewHolder Constructor with View and viewType as a parameter
    public ViewHolder(View itemView, int ViewType, ClickListener listener) {
        super(itemView);

        if (ViewType == TYPE_HEADER){
            itemView.setClickable(false);
            Name = (TextView) itemView.findViewById(R.id.user_name);
            email = (TextView) itemView.findViewById(R.id.user_email);
            profile = (ImageView) itemView.findViewById(R.id.circleView);

            Holderid = 0;
        }
        if(ViewType == TYPE_ITEM) {
            itemView.setClickable(true);
            textView = (TextView) itemView.findViewById(R.id.rowText);

            imageView = (ImageView) itemView.findViewById(R.id.rowIcon);

            Holderid = 1;

            // Handle item click and set the selection
        }
        if(ViewType == TYPE_SEPARATOR){
            itemView.setClickable(false);
            drawerTitle = (TextView) itemView.findViewById(R.id.drawerTitle);

            Holderid = 2;
        }
        this.listener = listener;
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
    }
    int pos;
    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClicked(getPosition());
            pos= getPosition();
            Log.d("TAG", "Item clicked at position " + getPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (listener != null) {
            return listener.onItemLongClicked(getPosition());
        }

        return false;
    }
    public interface ClickListener {
        public void onItemClicked(int position);
        public boolean onItemLongClicked(int position);
    }
}

我无法理解的是如何将ClickListener应用于MyAdapter对象:

        mAdapter = new MyAdapter(dataList, mSelectedPositions, ...?);

这样当用户点击一个项目时,我就可以在MainActivity处理用户的点击。

我已经以这种方式创建了适配器(如教程中所述):

整个MyAdapter课程:http://pastie.org/private/cibcsj3zieorlkcgh2rm1g

这是我的MainActivity类'onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Assigning the toolbar object ot the view
    and setting the the Action bar to our toolbar */
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //pulsante drawer
    getSupportActionBar().setHomeButtonEnabled(true);      //pulsante dietro

    //Initializing
    mTitle = mDrawerTitle = getTitle();
    SharedPreferences usrData = getSharedPreferences(
                                    usr_loggedin,
                                    MODE_PRIVATE);
    AVATARresID = R.mipmap.aka;

    // Add Drawer Item to dataList
    dataList = new ArrayList<>();
    dataList = prepareDatalist(dataList, NAME, EMAIL, AVATARresID);

    mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mAdapter = new MyAdapter(dataList, mSelectedPositions); //**ERROR HERE**

    mRecyclerView.setAdapter(mAdapter);
    mLayoutManager = new LinearLayoutManager(this);

    mRecyclerView.setLayoutManager(mLayoutManager);

    // Drawer Listener set to the Drawer toggle
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState(); // Finally we set the drawer toggle sync State
}

如何将onClick课程中出现的MyAdapter的结果提取回MainActivity

    int pos;
    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClicked(getPosition());
            pos= getPosition();
            Log.d("TAG", "Item clicked at position " + getPosition());
        }
    }

这样我可以根据点击的项目位置处理操作吗?

1 个答案:

答案 0 :(得分:1)

ClickListener的构造函数也是实现Activity的类的对象。例如,您可以让Fragment / this实现该接口并使用public class MainActivity extends Activity implements ViewHolder.ClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // .... mAdapter = new MyAdapter(dataList, mSelectedPositions, this); // .... } 作为参数

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fgps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my api key is here" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >     
        </activity>
        <activity android:name="AlarmReceiverActivity" android:label="@string/alarmReceiverActivity_string"></activity><activity
            android:name=".MnaActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.fgps.BackgroundLocationService" />
    </application>

</manifest>