如何使用片段在滑动选项卡布局中添加自定义列表视图

时间:2019-06-26 11:42:04

标签: android android-fragments android-listview

强文本我想在片段中添加列表视图并添加列表视图,但是它没有显示在片段布局内部,列表视图显示在整个页面上。

我尝试了很多方法,但仍然遇到此问题:

这是我的片段代码 公共类CrimeFragment扩展Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView=inflater.inflate(R.layout.crime_fragment,container,false);

    ListView lv= (ListView) rootView.findViewById(R.id.crimeListView);
    CustomAdapter adapter=new CustomAdapter(this.getActivity(),getCrimeMovies());
    lv.setAdapter(adapter);

    return rootView;
}

private ArrayList<TVShow> getCrimeMovies() {
    //COLECTION OF CRIME MOVIES
    ArrayList<TVShow> movies=new ArrayList<>();

    //SINGLE MOVIE
    TVShow tvShow=new TVShow("BLACKLIST",R.drawable.msg_bell);

    //ADD ITR TO COLLECTION
    movies.add(tvShow);

    tvShow=new TVShow("Breaking Bad",R.drawable.msg_bell);
    movies.add(tvShow);

    tvShow=new TVShow("Crisis",R.drawable.msg_bell);
    movies.add(tvShow);

    tvShow=new TVShow("BlackList",R.drawable.msg_bell);
    movies.add(tvShow);

    tvShow=new TVShow("Men In Black",R.drawable.msg_bell);
    movies.add(tvShow);

    return movies;
}

@Override
public String toString() {
    String title="Crime";
    return title;
}

}

这是我的适配器代码 公共类CustomAdapter扩展了BaseAdapter {

Context c;
ArrayList<TVShow> tvShows;
LayoutInflater inflater;

public CustomAdapter(Context c, ArrayList<TVShow> tvShows) {
    this.c = c;
    this.tvShows = tvShows;
}

@Override
public int getCount() {
    return tvShows.size();
}

@Override
public Object getItem(int position) {
    return tvShows.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(inflater==null)
    {
        inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.model,parent,false);
    }

    TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt);
    ImageView img= (ImageView) convertView.findViewById(R.id.movieImage);

    final String name=tvShows.get(position).getName();
    int image=tvShows.get(position).getImage();

    nameTxt.setText(name);
    img.setImageResource(image);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(c,name,Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

}

Screen shot

0 个答案:

没有答案