如题 初始化也做了 但是不知道是不是初始化的那个View是错误的 本人新手不太懂 下面是代码
Fragment
package com.lorentzos.flingswipe;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.shall.tantandemo.R;
import com.lorentzos.swipecards.RecyclerAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Sawano on 2016/7/16.
*/
public class FragmentOne extends Fragment {
private RecyclerView mRecyclerView;
LinearLayoutManager linearLayoutManager;
RecyclerAdapter mAdapter;
private List<Integer> mDatas;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDatas = new ArrayList<>(Arrays.asList(R.mipmap.back, R.mipmap.qiu, R.mipmap.img_1, R.mipmap.img_2,
R.mipmap.img_3, R.mipmap.img_4, R.mipmap.img_5, R.mipmap.img_6));
for(int i = 0; i < 15; i++) {
mDatas.add(i);
}
mAdapter = new RecyclerAdapter(this.getActivity(), mDatas);
Log.d(“hhhh”, “mRecyclerView=”+mRecyclerView);
Log.d(“hhhh”, “mAdapter=” + mAdapter);
linearLayoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
这是Adapter
Fragment
package com.lorentzos.flingswipe;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.shall.tantandemo.R;
import com.lorentzos.swipecards.RecyclerAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by Sawano on 2016/7/16.
*/
public class FragmentOne extends Fragment {
private RecyclerView mRecyclerView;
LinearLayoutManager linearLayoutManager;
RecyclerAdapter mAdapter;
private List<Integer> mDatas;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDatas = new ArrayList<>(Arrays.asList(R.mipmap.back, R.mipmap.qiu, R.mipmap.img_1, R.mipmap.img_2,
R.mipmap.img_3, R.mipmap.img_4, R.mipmap.img_5, R.mipmap.img_6));
for(int i = 0; i < 15; i++) {
mDatas.add(i);
}
mAdapter = new RecyclerAdapter(this.getActivity(), mDatas);
Log.d(“hhhh”, “mRecyclerView=”+mRecyclerView);
Log.d(“hhhh”, “mAdapter=” + mAdapter);
linearLayoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
这是Adapter
解决方案
10
mDatas = new ArrayList<>(Arrays.asList(R.mipmap.back, R.mipmap.qiu, R.mipmap.img_1, R.mipmap.img_2,
R.mipmap.img_3, R.mipmap.img_4, R.mipmap.img_5, R.mipmap.img_6));
for(int i = 0; i < 15; i++) {
mDatas.add(i);
}
关键之这个,你把mDatas已经初始化了几个img,但是又添加个for循环,加入15个int类型的值做什么?
R.mipmap.img_3, R.mipmap.img_4, R.mipmap.img_5, R.mipmap.img_6));
for(int i = 0; i < 15; i++) {
mDatas.add(i);
}
关键之这个,你把mDatas已经初始化了几个img,但是又添加个for循环,加入15个int类型的值做什么?
10
你没搞明白fragment的生命周期吧。onActivityCreate执行的时候,你没还没执行onCreateView呢,这个时候你的recyclerview当然是空的了
10
题主解决了么?
10
你那里应该用 onViewCreated 这样执行顺序才对