我试着在android中做一个关于Fragment
的例子我做了但我犯了一些错误。
当我将自定义ListView
显示在片段选项卡的选项卡中的项目列表时,我看到它显示非常正常,但是当我移动到另一个选项卡并返回到最后一个选项卡(链接列表)时
我看到它是列表* 2我的意思是双重列表ex :(如果我有两个项目,当我回来时,我看到4项!!)这是我的代码...
MainActivity Class
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import com.examples.borsacapital.R;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener{
private ViewPager viewPager;
private FragmentAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Main", "Stocks", "Links" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new FragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.Favorites :
Intent i = new Intent(this,Favorites.class);
startActivity(i);
break;
case R.id.CopyRights :
Intent ii = new Intent(this,CopyRights.class);
startActivity(ii);
break;
}
return super.onOptionsItemSelected(item);
}
链接类
import java.util.ArrayList;
import java.util.List;
import com.examples.borsacapital.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Links extends Fragment {
ListView listView;
List<LinksDetails> model = new ArrayList<LinksDetails>();
ArrayAdapter<LinksDetails> adapter1 = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_links,container ,false);
listView = (ListView) v.findViewById(R.id.list);
int y = listView.getCount();
if (y == 0) {
final String[] values = new String[] { "BorsaCapital", "Email", "Facebook",
"Twitter", "Google+", "Skype", "LinkedIn", "Yahoo",
"Amazon", "Blogyy", "SoundCloud", "Our Cloud", "Flaker" };
for (int x = 0; x < values.length; x++) {
LinksDetails L = new LinksDetails();
L.setLID(x);
L.setLName(values[x]);
L.setLImage("F");
adapter1 = new LinksRowActivity(getActivity(),
android.R.layout.simple_list_item_1, model);
adapter1.add(L);
}
listView.setAdapter(adapter1);
listView.setOnItemClickListener(Item_Click);
return v;
}
return null;
}
private OnItemClickListener Item_Click = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,final View view, int position,
long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = listView.getItemAtPosition(position).toString();
if (itemPosition == 0) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.borsacapital.com/"));
startActivity(i);
} else {
// Show Alert
Toast.makeText(
getActivity(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_SHORT).show();
}
}
};
class LinksRowActivity extends ArrayAdapter<LinksDetails> {
public LinksRowActivity(Context context, int resource,
List<LinksDetails> objects) {
super(context, resource, objects);
}
// problem here =======================
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinksHolder holder;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater(getArguments());
convertView = inflater.inflate(R.layout.row_links, parent,
false);
holder = new LinksHolder(convertView);
convertView.setTag(holder);
} else {
holder = (LinksHolder) convertView.getTag();
}
holder.populateForm(model.get(position));
return convertView;
}
}
LinksHolder Class
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class LinksHolder {
private TextView LinkName = null ;
private ImageView Icon = null;
LinksHolder(View row){
this.LinkName =(TextView)row.findViewById(R.id.txtTitle);
this.Icon = (ImageView)row.findViewById(R.id.iconImageView);
}
public void populateForm(LinksDetails LD){
LinkName.setText(LD.getLName());
switch(LD.getLID()){
case 0:
Icon.setImageResource(R.drawable.ic_launcher);
break;
case 1:
Icon.setImageResource(R.drawable.email);
break;
case 2:
Icon.setImageResource(R.drawable.facebook);
break;
case 3:
Icon.setImageResource(R.drawable.twitter);
break;
case 4:
Icon.setImageResource(R.drawable.google);
break;
case 5:
Icon.setImageResource(R.drawable.skype);
break;
case 6:
Icon.setImageResource(R.drawable.linkedin);
break;
case 7:
Icon.setImageResource(R.drawable.yahooo);
break;
case 8:
Icon.setImageResource(R.drawable.amazon);
break;
case 9:
Icon.setImageResource(R.drawable.blogger);
break;
case 10:
Icon.setImageResource(R.drawable.soundcloud);
break;
case 11:
Icon.setImageResource(R.drawable.cloud);
break;
case 12:
Icon.setImageResource(R.drawable.flickr);
break;
}
}
我认为getView()
方法在链接类中的问题打开此选项卡时此方法运行且位置参数= 13 !!