因为这是我的主要活动。
package com.appshep.fbalbumdownloader.activities;
public class ProfileActivity extends AppCompatActivity implements ProfileListener {
@Bind(R.id.tabs)
TabLayout tabLayout;
@Bind(R.id.viewpager)
ViewPager viewPager;
AccessToken token;
private String imageURL;
private String userID;
private String userName;
GraphRequest graphRequest;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//initialization
ButterKnife.bind(this);
token=AccessToken.getCurrentAccessToken();
userID=token.getUserId();
graphRequest= new GraphRequest(token, "/me", null, HttpMethod.GET, new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
try
{
if(graphResponse!=null)
{
JSONObject obj = graphResponse.getJSONObject();
userName= obj.getString("name");
imageURL = "https://graph.facebook.com/" + userID
+ "/picture?height=100&width=100";
DownloadManager manager= new DownloadManager(ProfileActivity.this);
//hack created
// manager.setStr(imageURL);
manager.execute(imageURL);
}
}
catch (Exception e){
e.printStackTrace();
}
}
});
graphRequest.executeAsync();
//Define the number of tabs by setting appropriate fragment and tab name.
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new LoggedinFragment(), "Home");
adapter.addFragment(new MyVideosFragment(), "Videos");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
@Override
public void onSuccess(final Bitmap bitmap) {
Bitmap output=getRoundedCornerBitmap(bitmap);
ImageView imageView=(ImageView)findViewById(R.id.profile_pic);
imageView.setImageBitmap(output);
}
@Override
public void onFailure(String string) {
}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 95;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
这是我有一个按钮的当前片段。并且我希望在点击它之后将此片段替换为另一个片段。我想我在替换Fragment或onCreateView()时遇到了问题。此外,我无法使用片段ID,因为我没有在布局中使用片段小部件。
public class LoggedinFragment extends android.support.v4.app.Fragment implements View.OnClickListener {
TextView textView;
SharedPreferences sharedPreferences;
String userName;
Button loadAlbums;
MyAlbumsFragment fragment;
GridOfPictures fragmentGrid;
FragmentManager manager;
android.support.v4.app.FragmentTransaction transaction;
public LoggedinFragment() {
//Empty constructor is required
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_loggedin, container, false);
getButton(view);
return view;
}
public void replaceFragment()
{
try
{
// Create new fragment and transaction
//fragment= new MyAlbumsFragment();
fragmentGrid= new GridOfPictures();
manager=getFragmentManager();
transaction=manager.beginTransaction();
//transaction.replace(R.id.com_facebook_fragment_container,fragment,"My Albums");
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
//transaction.replace(R.id.frameContainer, fragmentGrid, "Album");
transaction.replace(((ViewGroup)getView().getParent()).getId(), fragmentGrid, "My Albums");
transaction.addToBackStack(null);
transaction.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.my_photos_btn:
replaceFragment();
break;
case R.id.friends_photos_btn:
replaceFragment();
break;
case R.id.my_tags_btn:
replaceFragment();
break;
case R.id.tell_friend_btn:
replaceFragment();
break;
case R.id.more_apps_btn:
replaceFragment();
break;
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
textView=(TextView) view.findViewById(R.id.username);
sharedPreferences= getActivity().getSharedPreferences("Session", Context.MODE_PRIVATE);
if(sharedPreferences.contains("UserName"))
{
userName=sharedPreferences.getString("UserName","");
}
textView.setText(userName);
}
public void getButton(View view){
loadAlbums=(Button) view.findViewById(R.id.my_photos_btn);
loadAlbums.setOnClickListener(this);
}
}
这是我希望被我的第一个片段替换的片段,我以编程方式创建的片段不是布局中的片段小部件。
public class GridOfPictures extends Fragment {
//Empty constructor is required
public GridOfPictures() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.grid_of_pictures,container, false);
//Fetch that GridView
GridView gridView=(GridView) rootView.findViewById(R.id.gridView);
//Use rootView to get context instead of getActivity()
gridView.setAdapter( new ImageAdapter(rootView.getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getActivity(), "" + position,
Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
这是名为ImageAdapter的FragmentAdapter。
public class ImageAdapter extends BaseAdapter {
private Context context;
//Reference to our images
private Integer[] thumbIDs= {R.drawable.bride,R.drawable.images,R.drawable.feet,
R.drawable.tech, R.drawable.islamic, R.drawable.bangles};
public ImageAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return thumbIDs.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
ImageView imageView;
if(convertView==null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(320, 320));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
}
else
{
imageView=(ImageView)convertView;
}
imageView.setImageResource(thumbIDs[i]);
return imageView;
}
}
答案 0 :(得分:0)
尝试使用容器作为碎片。像这样创建一个新的片段:
my_container.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
MyContainer.java:
public class MyFragmentContainer extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.my_container,parent,false);
FragmentManager fm=getFragmentManager();
android.support.v4.app.Fragment fragment =new LoggedInFragment();
fm.beginTransaction().add(R.id.myContainer,fragment).commit();
return view;
}
}
这将是容纳您的片段的容器。替换容器内的片段将如下所示:
public void replaceFragment()
{
try
{
fragmentGrid= new GridOfPictures();
manager=getFragmentManager();
transaction=manager.beginTransaction();
transaction.replace(R.id.myContainer, fragmentGrid, "My Albums");
transaction.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
}