我的问题是1个活动有2个viewpager来改变片段。顶部viewpager与固定片段(没有问题),但底部viewpager与变量(不固定计数)片段。
例如:top viewpage有片段-1,片段-1有它的viewpager,可以用固定片段改变片段(没问题)。
我可以声明变量片段(例如:fragment_4作为模板片段),但我不知道如何在片段中获取/设置对象。
谁能帮我 ?我试了好几天但没有解决方案,谢谢
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import pl.droidsonroids.gif.GifImageView;
public class Fragment1 extends Fragment {
ViewPager pager;
ArrayList<View> pagerList;
private fragment_4 fragment4 = new fragment_4();
private fragment_5 fragment5 = new fragment_5();
private ListView lv;
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_1, container, false);
final List<Fragment> FragArr = new ArrayList<Fragment>();
FragArr.add(fragment4);
FragArr.add(fragment5);
pager = (ViewPager)view.findViewById(R.id.viewPager);
pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
return FragArr.get(position);
//return null;
}
@Override
public int getCount() {
return FragArr.size();
}
});
/* Set Data From Server */
TextView user_tv = (TextView)view.findViewById(R.id.family_name);
String mammy = getResources().getString(R.string.mammy);
user_tv.setText(mammy);
ImageView user_iv = (ImageView)view.findViewById(R.id.main_img);
user_iv.setImageResource(R.drawable.mammy);
ImageView lock_iv = (ImageView)view.findViewById(R.id.lock_img);
lock_iv.setImageResource(R.drawable.lock);
ImageView battery_iv = (ImageView)view.findViewById(R.id.battery_img);
battery_iv.setImageResource(R.drawable.battery_full);
GifImageView giv = (GifImageView)view.findViewById(R.id.status_img);
giv.setImageResource(R.drawable.away_always);
TextView status_time_tv = (TextView)view.findViewById(R.id.status_time);
String min = getResources().getString(R.string.minute);
status_time_tv.setText("15 "+min);
return view;
}
}