我似乎找不到我遇到的问题的问题,即当我导航到ResidenceFrag片段时,在实现了回收视图后它将停止工作。只是片段工作得很好,但是在实现回收视图后,该视图不再起作用。以下是我的文件。谢谢。

ResidenceFrag.java

package com.example.ezhome;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class ResidenceFrag extends Fragment {

    View v;
    MHS mhs;
    private RecyclerView recyclerView;
    private ArrayList<Residence> resList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.residence_frag, container, false);

        mhs = new MHS();
        resList = mhs.getResidences();

        Collections.sort(resList, new Comparator<Residence>(){
            @Override
            public int compare(Residence r1, Residence r2){
                return r1.getName().compareTo(r2.getName());
            }
        });

        recyclerView = (RecyclerView) v.findViewById(R.id.rvItems);
        ViewResidenceAdapter adapter = new ViewResidenceAdapter(getContext(),resList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        return v;
    }
}


MHS.java

package com.example.ezhome;

import java.util.ArrayList;

public class MHS {
    public ArrayList<Residence> residences;
    public Applicant applicant;

    public MHS() {
        setResidences(new ArrayList<Residence>());

        residences.add(new Residence("ResB","A_address123",12,200,300,"micro1.jpg"));
        residences.add(new Residence("ResA","B_address123",17,228,390,"micro2.jpg"));
    }

    public void submitApp(Application app){
        applicant.addApplication(app);
    }

    public ArrayList<Residence> getResidences() {
        return residences;
    }

    public void setResidences(ArrayList<Residence> residences) {
        this.residences = residences;
    }
}


Residence.java

package com.example.ezhome;

import java.util.ArrayList;

public class Residence {
    private static int nextResidenceID = 1;
    private String residenceID, address;
    private int numUnits, i;
    private double sizePerUnit, monthlyRental;
    private ArrayList<Unit> units;
    private String name;
    private String image;

    public Residence(String name, String address, int numUnits, double sizePerUnit, double monthlyRental, String image) {
        setName(name);
        setAddress(address);
        setNumUnits(numUnits);
        setSizePerUnit(sizePerUnit);
        setMonthlyRental(monthlyRental);
        setResidenceID();
        setImage(image);
//        setUnits(new ArrayList<Unit>());

//        for(i = 0; i < numUnits; i++){
//            Unit unit = new Unit(this);
//        }
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static int getNextResidenceID() {
        return nextResidenceID;
    }

    public static void setNextResidenceID(int nextResidenceID) {
        Residence.nextResidenceID = nextResidenceID;
    }

    public String getResidenceID() {
        return residenceID;
    }

    public void setResidenceID() {
        this.residenceID = String.format("RES" + nextResidenceID++);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getNumUnits() {
        return numUnits;
    }

    public void setNumUnits(int numUnits) {
        this.numUnits = numUnits;
    }

    public double getSizePerUnit() {
        return sizePerUnit;
    }

    public void setSizePerUnit(double sizePerUnit) {
        this.sizePerUnit = sizePerUnit;
    }

    public double getMonthlyRental() {
        return monthlyRental;
    }

    public void setMonthlyRental(double monthlyRental) {
        this.monthlyRental = monthlyRental;
    }

    public ArrayList<Unit> getUnits() {
        return units;
    }

    public void setUnits(ArrayList<Unit> units) {
        this.units = units;
    }

    public void addUnit(Unit unit) {
        getUnits().add(unit);
    }
}


ViewResidenceAdapter.java

package com.example.ezhome;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
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.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class ViewResidenceAdapter extends RecyclerView.Adapter<ViewResidenceAdapter.ViewHolder>{

    private ArrayList<Residence> resList;
    private Context context;

    public ViewResidenceAdapter(Context context, ArrayList<Residence> resList){
        this.context = context;
        this.resList = resList;
    }

    @Override
    public ViewResidenceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflater = LayoutInflater.from(context).inflate(R.layout.customlayout, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewResidenceAdapter.ViewHolder holder, int position) {
        Residence res = resList.get(position);

        try{
            holder.nameTextView.setText(res.getName());
            holder.sizeTextView.setText(res.getSizePerUnit() + " sqft");
            holder.addressTextView.setText(res.getAddress());
            holder.monthlyRentalTextView.setText("RM " + res.getMonthlyRental() + "/month");
            holder.applyButton.setTag(res.getResidenceID());
            String imageFile = res.getImage();
            InputStream inputStream = context.getAssets().open(imageFile);
            Drawable d = Drawable.createFromStream(inputStream, null);
            holder.resImg.setImageDrawable(d);
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public int getItemCount() {
        return resList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView nameTextView;
        public TextView sizeTextView;
        public TextView addressTextView;
        public TextView monthlyRentalTextView;
        public ImageView resImg;
        public Button applyButton;
        public ViewHolder (View itemView){
            super(itemView);

            nameTextView = (TextView)itemView.findViewById(R.id.nameTextView);
            sizeTextView = (TextView)itemView.findViewById(R.id.sizeTextView);
            addressTextView = (TextView)itemView.findViewById(R.id.addressTextView);
            monthlyRentalTextView = (TextView)itemView.findViewById(R.id.monthlyRentalTextView);
            resImg = (ImageView)itemView.findViewById(R.id.resImg);
            applyButton = (Button)itemView.findViewById(R.id.applyButton);
        }
    }
}


MainActivity.java

package com.example.ezhome;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.nav_drawer_open, R.string.nav_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFrag()).commit();
            navigationView.setCheckedItem(R.id.nav_home);
        }


    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == R.id.nav_home){
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFrag()).commit();
        } else if (item.getItemId() == R.id.nav_res){
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new ResidenceFrag()).commit();
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

最佳答案

解决:可绘制图像v24问题

关于java - 已解决:Android Studio:在片段中实现回收 View 时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58568135/

10-12 06:05