FindViewById在不同的布局?

时间:2017-07-31 10:23:32

标签: android

我必须布局,main_activity和其他layout2,我在layout2中有一个按钮和EditText ..我想从我的main_activity调用layout2中的Button和EditText,无论如何要做到这一点? 这是我的布局2:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<android.support.v7.widget.CardView
android:id="@+id/ccard_view"
android:layout_width="match_parent"
android:layout_height="315dp"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="226dp"
    android:orientation="vertical"
    android:weightSum="1">
    <TextView
        android:id="@+id/title_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="7dp"
        android:text="this is title"
        android:textColor="@color/primaryText"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_weight="0.31"/>

    <TextView
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="7dp"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="this is lorem ipsum content of universe"
        android:textColor="@color/secondaryText"
        android:textSize="17sp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:id="@+id/response"/>
    <Button
        android:id="@+id/save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:text="Save"


        />

</LinearLayout>

这是我的main_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dell.offlinesurvey.MainActivity"
android:id="@+id/relativelayout">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

Button和EditText在layout2中,它是一个显示数据的cardview。

我的适配器类:

package com.example.dell.offlinesurvey;
import android.app.Dialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;

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

import static com.example.dell.offlinesurvey.R.id.response;
import static com.example.dell.offlinesurvey.R.id.save;
import static com.example.dell.offlinesurvey.R.id.show;



public class MyRecyclerAdapter extends 
RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {

private List<NewsQuestions> questionsList;
private Context context;
private LayoutInflater inflater;
private  SQLiteDatabase db;




public MyRecyclerAdapter(Context context, List<NewsQuestions> questionsList) {

    this.context = context;
    this.questionsList = questionsList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}




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

    View rootView = inflater.inflate(R.layout.singleitem_recyclerview, parent, false);

    return new MyViewHolder(rootView);


}





@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    NewsQuestions questions = questionsList.get(position);
    //Pass the values of feeds object to Views
    holder.question.setText(questions.getQuestion());
    holder.qid.setText(questions.getQid());

   /** holder.imageview.setImageUrl(feeds.getImgURL(), NetworkController.getInstance(context).getImageLoader());
    holder.ratingbar.setProgress(feeds.getRating());**/
}

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



public class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView question, qid;
    private EditText rp;
    private Button save;
    private NetworkImageView imageview;
    private ProgressBar ratingbar;

    public MyViewHolder(View itemView) {
        super(itemView);
        question = (TextView) itemView.findViewById(R.id.title_view);
        qid = (TextView) itemView.findViewById(R.id.content_view);






        // Volley's NetworkImageView which will load Image from URL
       // imageview = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
       // ratingbar = (ProgressBar) itemView.findViewById(R.id.ratingbar_view);
       /** ratingbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Rated By User : " + questionsList.get(getAdapterPosition()).getRating(), Toast.LENGTH_SHORT).show();
            }
        });**/

    }
}

}

3 个答案:

答案 0 :(得分:0)

尝试在onBindViewHolder方法

中添加此代码
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
   holder.btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(Main2Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

对于recyclerview的演示,请查看此androidhive recyclerview demo

如果有任何疑问,请询问我

答案 1 :(得分:0)

You need to create an adapter class like

    public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {

    private List<Movie> moviesList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, year, genre;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            genre = (TextView) view.findViewById(R.id.genre);
            year = (TextView) view.findViewById(R.id.year);
        }
    }


    public MoviesAdapter(List<Movie> moviesList) {
        this.moviesList = moviesList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.movie_list_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Movie movie = moviesList.get(position);
        holder.title.setText(movie.getTitle());
        holder.genre.setText(movie.getGenre());
        holder.year.setText(movie.getYear());
    }

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

and then Your MainActivity will look like

    public class MainActivity extends AppCompatActivity {
    private List<Movie> movieList = new ArrayList<>();
    private RecyclerView recyclerView;
    private MoviesAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

        mAdapter = new MoviesAdapter(movieList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        prepareMovieData();
    }

    private void prepareMovieData() {
        Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
        movieList.add(movie);

        movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");
        movieList.add(movie);

        movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
        movieList.add(movie);

        movie = new Movie("Shaun the Sheep", "Animation", "2015");
        movieList.add(movie);

        movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");
        movieList.add(movie);

        movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");
        movieList.add(movie);

        movie = new Movie("Up", "Animation", "2009");
        movieList.add(movie);

        movie = new Movie("Star Trek", "Science Fiction", "2009");
        movieList.add(movie);

        movie = new Movie("The LEGO Movie", "Animation", "2014");
        movieList.add(movie);

        movie = new Movie("Iron Man", "Action & Adventure", "2008");
        movieList.add(movie);

        movie = new Movie("Aliens", "Science Fiction", "1986");
        movieList.add(movie);

        movie = new Movie("Chicken Run", "Animation", "2000");
        movieList.add(movie);

        movie = new Movie("Back to the Future", "Science Fiction", "1985");
        movieList.add(movie);

        movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");
        movieList.add(movie);

        movie = new Movie("Goldfinger", "Action & Adventure", "1965");
        movieList.add(movie);

        movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
        movieList.add(movie);

        mAdapter.notifyDataSetChanged();
}
}

您可以根据需要参考以下网址 enter link description here

答案 2 :(得分:0)

您可以在MyViewHolder中添加onclicklistener

public MyViewHolder(View itemView) {
    super(itemView);
    question = (TextView) itemView.findViewById(R.id.title_view);
    qid = (TextView) itemView.findViewById(R.id.content_view);
    rp = (EditText) itemView.findViewById(R.id.rp);
    save = (Button) itemView.findViewById(R.id.save);
    save..setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    questionsList.get(getAdapterPosition()).setQuestion(rp.getText().toString());
                }
            });


}