使用片段

时间:2018-06-16 02:25:19

标签: android android-recyclerview android-fragmentactivity recycler-adapter

目前,我正在开发一个带有导航抽屉的项目,我正在使用片段。我准备为它使用循环视图,但是我遇到了代码问题,特别是当我尝试添加LayoutInflater时,并将其粘贴到我应该使用layoutinflater.from(fragment.getBaseContext);的片段中不工作

这是适用于常用Activity的代码,但它似乎不适用于.fragment活动

有人可以帮我这个吗?甚至指出我正确的方向?

package sample.101.sample.fragment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewHolder> {

    Bad_house_list bad_house_list;
    ArrayList<User> userArrayList;

    public MyRecycleViewAdapter(Bad_house_list bad_house_list, ArrayList<User> userArrayList) {
        this.bad_house_list = bad_house_list;
        this.userArrayList = userArrayList;
    }

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

        LayoutInflater layoutInflater = LayoutInflater.from(Bad_house_list.getBaseContext());
        layoutInflater.inflate(R.layout.single_row, parent,false);


        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecycleViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个!

import UIKit

class GradientView: UIView {

var gradientLayer = CAGradientLayer()

    override init(frame: CGRect) {

        super.init(frame:frame)

        self.backgroundColor = .orange
        gradientLayer.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
        gradientLayer.locations = [0.6, 1.0]
        gradientLayer.frame = self.bounds
        self.layer.insertSublayer(gradientLayer, at: 0)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        self.gradientLayer.frame = self.bounds
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}

答案 1 :(得分:1)

尝试用此

替换您的适配器
package sample.101.sample.fragment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewHolder> {

Bad_house_list bad_house_list;
ArrayList<User> userArrayList;

public MyRecycleViewAdapter(Bad_house_list bad_house_list, ArrayList<User> userArrayList) {
    this.bad_house_list = bad_house_list;
    this.userArrayList = userArrayList;
}

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

 View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row, parent, false);
 return new MyRecycleViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MyRecycleViewHolder holder, int position) {

}

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