我将一个活动扩展为Fragment并实现了TabListner。我可以在该活动中实现TabHost(而不是Fragment TabHost)。因为当我尝试时,我找不到findViewById中的tabhost。
UserProfile.java
public class UserProfile extends Fragment implements TabListener{
private static final int RESULT_OK = -1;
private static int RESULT_LOAD_IMAGE = 1;
Map<String, AttributeValue> map = new HashMap<String, AttributeValue>();
TabHost mTabHost;
ImageView profilepic;
String userId = new PassValues().getUserId();
ArrayList<UserDetailsBean> ar = new ArrayList<UserDetailsBean>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_user_profile,
container, false);
TextView name = (TextView) rootView.findViewById(R.id.name);
//profilepic = (ImageView) rootView.findViewById(R.id.profile_pic);
/*mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabhost);
mTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.tabcontent1);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
UserDetailsActivity.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
UserJoinedGroupsActivity.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
UserPostedDiscussionActivity.class, null);*/
AsyncTask<String, String, ArrayList<UserDetailsBean>> async = new GetUserDetailsDataSource()
.execute(userId);
try {
ar = async.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < ar.size(); i++)
name.setText(ar.get(i).getFirstname() + " "
+ ar.get(i).getLastname());
profilepic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
System.out.println(selectedImage);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
System.out
.println((new PassValues().getUserId() + new LoginSharePreferences()
.getEmailId(getActivity())));
System.out.println(ar.get(0).getProfilePicPath());
AsyncTask<String, Void, Map<String, AttributeValue>> async = new InterestGroupsListDataSource(
userId, picturePath,
new LoginSharePreferences().getEmailId(getActivity()), ar
.get(0).getProfilePicPath(), "profile").execute();
try {
map = async.get();
System.out.println(map.get("Profile Image Path").getS());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// profilepic.setImageBitmap(BitmapFactory.decodeFile(map.get("Profile Image Path").getS()));
profilepic.setImageBitmap(BitmapFactory.decodeFile(ar.get(0)
.getProfilePicPath()));
}
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_user_profile.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdgeLength="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".UserProfile" >
<ImageView
android:id="@+id/profile_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="43dp"
android:minWidth="100dp"
android:src="@drawable/button_shape_google" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/profile_pic"
android:layout_marginTop="23dp"
android:layout_toRightOf="@+id/profile_pic"
android:text="Anubhav Bhardwaj"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_marginTop="180dp"
android:layout_height="0dip"
android:layout_weight="1">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" >
</TabWidget>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
答案 0 :(得分:0)
您可以将活动扩展为TabActivity以在程序中获取tabhost方法。如果您的活动扩展为FragmentActivity,您可以实现任何侦听器类,但您可以在该活动中获取Tabhost。
只需将您的活动更改为
Public class *youactivity* extends TabActivity implements TabListener{
*Your code*
}
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TabHost>