Recycler视图中的Imageview在每个位置加载相同的图像

时间:2015-11-07 08:40:51

标签: java android android-recyclerview

我创建了一个包含RecyclerView的MainActivity。然后点击它将转到detailActivity页面。我在类Projects中创建了一组textview和一个图像值。这里每个textview都根据Recyclerview的位置正常工作。但。这里我的问题是Recycler视图中的所有imageview都加载了相同的Image。

MainActivity.java

package com.tutsplus.code.sharedelementtransition;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import jp.wasabeef.recyclerview.animators.LandingAnimator;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        RecyclerView rv = (RecyclerView) findViewById(R.id.rv); // layout reference
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
        rv.setItemAnimator(new LandingAnimator());
        rv.setHasFixedSize(true); // to improve performance




        rv.setAdapter(new DataManager()); // the data manager is assigner to the RV
        rv.addOnItemTouchListener( // and the click is handled
            new RecyclerClickListener(this, new RecyclerClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {
                    Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
                    intent.putExtra(DetailsActivity.ID, Projects.PROJECTS[position].getId());
                    startActivity(intent);
                }
                }));
    }

    }

DetailActivty.java

package com.tutsplus.code.sharedelementtransition;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class DetailsActivity extends Activity {

    public final static String ID = "ID";
    public Projects mProjects;

    public TextView mName, mPhone, mEmail, mCity;

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

        // Retrieve data
        mProjects = Projects.getItem(getIntent().getIntExtra(ID, 0));

        // Views
        mName = (TextView) findViewById(R.id.DETAILS_name);
        mPhone = (TextView) findViewById(R.id.DETAILS_phone);
        mCity = (TextView) findViewById(R.id.DETAILS_city);
        mEmail = (TextView) findViewById(R.id.DETAILS_email);


        // Data population
        mName.setText(mProjects.get(Projects.Field.NAME));
        mPhone.setText(mProjects.get(Projects.Field.PHONE));
        mCity.setText(mProjects.get(Projects.Field.DISTRICT));
        mEmail.setText(mProjects.get(Projects.Field.CITY));


    }

}

Projects.java

package com.tutsplus.code.sharedelementtransition;
public class Projects {

    // I am going to define here the data, public static and constant,
    // so that every class in the project will be able to read it.
    // In a sense, we just mimic the standard behaviour of a database.
    public static final Projects[] PROJECTS = new Projects[] {
        new Projects("Mir Project1", R.drawable.a, "+01 123456789", "Trivandrum", "East Fort"),
        new Projects("Mir Project2", R.drawable.b, "+01 987654321", "Pathanamthitta", "Variyapuram"),
        new Projects("Mir Project3", R.drawable.c, "+01 123456789", "Kottayam", "Pala"),
        new Projects("Mir Project4", R.drawable.d, "+01 987654321", "Kollam", "Anchal"),
        new Projects("Mir Project5", R.drawable.e, "+01 11235813", "Ernakulam", "Kadavanthra"),
        new Projects("Mir Project6", R.drawable.a, "+01 123456789", "Kannur", "Onjiyam"),
        new Projects("Mir Project7", R.drawable.b, "+01 11235813", "Alapuzha", "Chengannur"),
        new Projects("Mir Project8", R.drawable.c, "+01 987654321", "Idukki", "Peerumedu"),
    };

    // The fields associated to the person
    private final String mProjectName;
    private final String mProjectPhone;
    private final String mProjectDistrict;
    private final String mProjectCity;
    private static int mImage;

    Projects(String name, int image, String phone, String email, String city) {
        mProjectName = name; mImage = image; mProjectPhone = phone; mProjectDistrict = email; mProjectCity = city;
    }

    // This method allows to get the item associated to a particular id,
    // uniquely generated by the method getId defined below
    public static Projects getItem(int id) {
        for (Projects item : PROJECTS) {
            if (item.getId() == id) {
                return item;
            }
        }
        return null;
    }

    // since mName and mPhone combined are surely unique,
    // we don't need to add another id field
    public int getId() {
        return mProjectName.hashCode() + mProjectPhone.hashCode();
    }

    public static enum Field {
        NAME, PHONE, DISTRICT, CITY, IMAGE
    }
    public String get(Field f) {
        switch (f) {
            case PHONE: return mProjectPhone;
            case DISTRICT: return mProjectDistrict;
            case CITY: return mProjectCity;
            case NAME: default: return mProjectName;
        }
    }
    public int geti(Field f) {
        switch (f) {
            case IMAGE:

        } return mImage;
    }}

DataManager.java

package com.tutsplus.code.sharedelementtransition;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class DataManager extends RecyclerView.Adapter<DataManager.RecyclerViewHolder> {

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView mProjectName, mProjectCity, mProjectDistrict;
        ImageView mImage;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
            mProjectCity = (TextView) itemView.findViewById(R.id.PROJECT_city);
            mProjectDistrict = (TextView) itemView.findViewById(R.id.PROJECT_district);
            mImage = (ImageView) itemView.findViewById(R.id.CONTACT_circle);
        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.contact_item, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder viewHolder, int i) {
        // get the single element from the main array
        final Projects projects = Projects.PROJECTS[i];
        // Set the values
        viewHolder.mProjectName.setText(projects.get(Projects.Field.NAME));
        viewHolder.mProjectCity.setText(projects.get(Projects.Field.CITY));
        viewHolder.mProjectDistrict.setText(projects.get(Projects.Field.DISTRICT));
        viewHolder.mImage.setImageResource(projects.geti(Projects.Field.IMAGE));

    }





    @Override
    public int getItemCount() {

        return Projects.PROJECTS.length;
    }
}

1 个答案:

答案 0 :(得分:1)

下面:

private static int mImage;

mImagestatic,它与类(Projects)相关联,而不是该类的对象。