动态添加
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
protected void addtextview(Daohang fromJson) {
al = new ArrayList<TextView>();
al.clear();
ll.removeAllViews();
for (int i = 0; i < fromJson.result.size(); i++) {
final TextView text = new TextView(MainActivity.this);
text.setText(fromJson.result.get(i).name);
text.setTextSize(20);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(10, 5, 10, 5);
ll.addView(text, params);
al.add(text);
// 给text设置id
text.setId(i);
// 点击监听
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < al.size(); j++) {
if (j == v.getId()) {
al.get(j).setTextColor(Color.RED);
} else {
al.get(j).setTextColor(Color.BLACK);
}
}
}
});
}
}
设置fragment
MainActivity
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, arg0.getMessage(), 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
Gson gson = new Gson();
final Daohang fromJson = gson.fromJson(result, Daohang.class);
addtextview(fromJson);
viewPager.setAdapter(new FragmentPagerAdapter(
getSupportFragmentManager()) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return fromJson.result.size();
}
@Override
public Fragment getItem(int arg0) {
Fragment fragment = new F1(fromJson.result.get(arg0).id);
return fragment;
}
});
}
});
Fragment里的代码如下:
public class F1 extends Basefragment {
int id;
public F1(int id) {
this.id = id;
}
@Override
public String getidinfo() {
// TODO Auto-generated method stub
return id + "";
}
}
fragment适配器Basefragment代码如下:
public abstract class Basefragment extends Fragment {
private View view;
private ListView listView;
private EditText et;
private Button sou;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.item, container, false);
et = (EditText) view.findViewById(R.id.et);
listView = (ListView) view.findViewById(R.id.listView);
sou = (Button) view.findViewById(R.id.sou);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
String path = "http://japi.juhe.cn/health_knowledge/infoList?key=6a0e31fabaa7a98a4dfb4ca2cad91c44&id="
+ getidinfo();
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), arg0.getMessage(), 0).show();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String result = arg0.result;
Gson gson = new Gson();
News fromJson = gson.fromJson(result, News.class);
ArrayList<DD> data = fromJson.result.data;
listView.setAdapter(new MyAdapter(getActivity(), data));
}
});
}
public abstract String getidinfo();
}