我有一个活动,其中包含一个 ActionBar(带有四个选项卡),一个片段分别分配给每个选项卡。在这些片段中,我分配了一些填充了字符串值的 ListAdapter,这些值是我想要操作的可点击的。单击一个项目时,我希望该应用程序从该片段发送到另一个。我知道我必须使用 FragmentManager() 和 FragmentTransaction() 但由于我是 Android 开发新手,我需要任何形式的帮助,感谢您的帮助。
这是其中一个选项卡(UserFragment.java)的片段代码:
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
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;
/** This is a listfragment class */
public class UserFragment extends ListFragment
{
/** An array of items to display in ArrayList */
String user_items[] = new String[]
{
"Account",
"Addresses",
"Payment Providers",
"Profile",
"Transactions",
"Wallet"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_expandable_list_item_1, user_items);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
Intent myIntent = new Intent(getActivity().getBaseContext(), Profile.class);
startActivity(myIntent);
}
});
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart()
{
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
Profile.java 活动代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Profile extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_layout);
//Button test = (Button) findViewById(R.id.btnTest);
}
}