RecyclerView点击项目,获取一些数据然后转到另一个活动

时间:2017-03-23 12:13:28

标签: android api mobile android-recyclerview

当我点击此Recyclerview中的项目然后转到另一个带有此引用的活动时,我想要参考,列表视图中有覆盖功能,但是recylclerview没有相同的功能

the recyclerview

card.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="119dp"
        android:layout_gravity="center"
        android:elevation="3dp"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="4dp"
        card_view:cardCornerRadius="1dp"
        >

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

            <TextView
                android:text="ID Article : "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_idArticle1"
                android:textStyle="normal|bold"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textColor="@android:color/black" />
            <TextView
                android:text="Reference : "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_reference1"
                android:textStyle="normal|bold"

                android:layout_above="@+id/txt_des"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginBottom="5dp"
                android:textColor="@android:color/background_dark" />
            <TextView
                android:text="Prix : "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_des1"
                android:textStyle="normal|bold"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginBottom="-51dp"
                android:layout_below="@+id/txt_des"
                android:textColor="@android:color/background_dark" />
            <TextView
                android:text="TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_des"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/txt_prix1"
                android:layout_toEndOf="@+id/txt_prix1" />
            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txt_prix"
                android:layout_alignBaseline="@+id/txt_des1"
                android:layout_alignBottom="@+id/txt_des1"
                android:layout_toRightOf="@+id/txt_des1"
                android:layout_toEndOf="@+id/txt_des1" />
            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txt_idArticle"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/txt_idArticle1"
                android:layout_toEndOf="@+id/txt_idArticle1" />
            <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txt_reference"
                android:layout_alignBaseline="@+id/txt_reference1"
                android:layout_alignBottom="@+id/txt_reference1"
                android:layout_toRightOf="@+id/txt_reference1"
                android:layout_toEndOf="@+id/txt_reference1" />
            <TextView
                android:text="Designation : "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_prix1"
                android:textStyle="normal|bold"
                android:textColor="@android:color/black"
                android:layout_below="@+id/txt_reference1"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="5dp"
                android:layout_alignParentStart="true" />
        </RelativeLayout>
    </android.support.v7.widget.CardView> </LinearLayout>

avticity_recherche_art.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:article_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_recherche_art"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.bacha.pfe.activity.recherche_art">
        <TextView
            android:text="Recherche Article :"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:textSize="24sp"
            android:textStyle="normal|bold"
            android:textAlignment="center"
            android:textColor="?attr/colorPrimary"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
        <Button
            android:text="rechercher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnrechArt"
            android:layout_marginRight="20dp"
            android:layout_marginEnd="20dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/eTextRechArt" />


        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/eTextRechArt"
            android:layout_below="@+id/textView"
            android:layout_toLeftOf="@+id/btnrechArt"
            android:layout_toStartOf="@+id/btnrechArt" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            article_view:cardUseCompatPadding="true"
            article_view:cardElevation="5dp"
            article_view:cardCornerRadius="5dp"
            android:scrollbars="vertical"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/btnrechArt"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

recherche_art.java:

package com.example.bacha.pfe.activity;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.bacha.pfe.R; 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class recherche_art extends AppCompatActivity {
    private RecyclerView recyclerView ;
    private GridLayoutManager gridLayoutManager;
    private ArticleAdapter adapter ;
    private List<Article> data_list ;
    private String recherche_article;
    private Button btnrechArt ;
    private EditText arech;
    int d=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recherche_art);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

   btnrechArt = (Button) findViewById(R.id.btnrechArt);   
        btnrechArt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arech= (EditText) findViewById(R.id.eTextRechArt);
                data_list  = new ArrayList<>();
                recherche_article=arech.getText().toString();

                load_article_from_server(0);
                gridLayoutManager = new GridLayoutManager(recherche_art.this,1);
                recyclerView.setLayoutManager(gridLayoutManager);

                adapter = new ArticleAdapter(recherche_art.this,data_list);
                recyclerView.setAdapter(adapter);

                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                     if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
                            d=data_list.size();
                            Log.d("PFE", String.valueOf(d));
                            load_article_from_server(d);
                        }

                    }
                });

            }
        });

    }

    private void load_article_from_server(final int id) {

        AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer,Void, Void>() {
            @Override
            protected Void doInBackground(Integer... Params) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("http://10.0.2.2/slim/article/"+recherche_article+"/"+id).build();
                try {
                    Response response = client.newCall(request).execute();

                    JSONArray array = new JSONArray(response.body().string());

                    for(int i=0;i<array.length();i++){
                        JSONObject object = array.getJSONObject(i);
                        Article data = new Article(/*dd,*/object.getString("id_Article"),object.getString("Reference"),object.getString("Designation"),object.getString("PVTTC"));
                        data_list.add(data);

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    System.out.print("End of content");
                }


                return  null ;
            }



            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                adapter.notifyDataSetChanged();
            }
        };

        task.execute(id);

    }
}

1 个答案:

答案 0 :(得分:0)

您必须在ArticleAdapter中使用的持有者中实现侦听器。

看看这个:RecyclerView onClick