再次调用“onCreateView” - Android
Posted
技术标签:
【中文标题】再次调用“onCreateView” - Android【英文标题】:call "onCreateView" again - Android 【发布时间】:2018-12-02 13:38:36 【问题描述】:我正在尝试调用函数“onCreateView”(在第一个代码中)并在下面的第二个代码中调用它。 我需要在代码运行时将片段添加到堆栈中。 如果有其他好的方法,我很乐意听到。
关于函数的小解释: 该函数发送一些参数来创建一个新的卡片视图。 在接收参数的函数中,有一个循环使用“get count”创建卡片堆栈。 我需要在运行时更改“获取计数”...
感谢您的每一个帮助!
import android.app.Activity;
import android.graphics.drawable.Drawable;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import java.util.Random;
public class RecentsFragment extends Fragment
public int child_count = 3;
View ContainerContent;
boolean Appearance;
static RecentsFragment newInstance()
RecentsFragment f = new RecentsFragment();
return f;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View v = inflater.inflate(R.layout.materialrecents_activity_recents, container, false);
ContainerContent = getLayoutInflater().inflate(R.layout.activity_recents, container, false);
final int[] colors = new int[]0xff7fffff, 0xffff7fff, 0xffffff7f, 0xff7f7fff, 0xffff7f7f, 0xff7fff7f;
final Random random = new Random();
RecentsList recents = (RecentsList) v.findViewById(R.id.recents);
recents.setAdapter(new RecentsAdapter()
@Override
public String getTitle()
return "Item ";
@Override
public View getView()
ImageView iv = new ImageView(getActivity());
//iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setImageResource(R.drawable.room_back);
//iv.setBackgroundColor(R.drawable.mazda);
return iv;
@Override
public Drawable getIcon()
return getResources().getDrawable(R.mipmap.ic_launcher);
@Override
public int getHeaderColor()
return 0xff9900;
@Override
public int getCount()
return child_count;
);
recents.setOnItemClickListener(new RecentsList.OnItemClickListener()
@Override
public void onItemClick(View view, int i)
onItemExpand(Appearance,view);
//Toast.makeText(view.getContext(), "Card " + i + " clicked", Toast.LENGTH_SHORT).show();
);
return v;
public void onItemExpand(boolean b,View view)
//mContainerContent.setVisibility(b ? View.VISIBLE: View.GONE);
if (b)
ViewAppearByEffect(ContainerContent);
Toast.makeText(view.getContext(), "Card " + " clicked", Toast.LENGTH_SHORT).show();
Appearance = !Appearance;
else
ContainerContent.setVisibility(View.GONE);
Appearance = !Appearance;
public void ViewAppearByEffect(View view)
view.setAlpha(0);
view.setVisibility(View.VISIBLE);
view.animate().alpha(1.0f).setDuration(300);
//view.animate().rotation(360);
public void addCount()
child_count++;
主要活动:
import android.app.Activity;
import android.app.ActivityOptions;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
public class RecentsActivity extends FragmentActivity
MyAdapter mAdapter;
ViewPager mPager;
//RecentsFragment firstFragment = new RecentsFragment();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
// Watch for button clicks.
Button button = (Button) findViewById(R.id.goto_first);
button.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
mPager.setCurrentItem(0);
//firstFragment.setArguments(getIntent().getExtras());
//firstFragment.addCount();
//getSupportFragmentManager().beginTransaction().add(R.id.pager, firstFragment);
);
button = (Button) findViewById(R.id.goto_last);
button.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
mPager.setCurrentItem(mAdapter.getCount() - 1);
);
【问题讨论】:
手动调用Activity
/Fragment
回调,如onCreateView
/onCreate
是错误的。您必须创建一个函数,在其中执行请求的操作并从onCreateView
调用它,并且每当您想重新执行代码时
【参考方案1】:
只需创建一个方法ex。 updateUI 将您的添加卡片或更新数据逻辑放在此方法中,每当您获取数据时,然后将数据传递给此方法。
【讨论】:
以上是关于再次调用“onCreateView” - Android的主要内容,如果未能解决你的问题,请参考以下文章
为啥popbackstack会调用fragment的onCreateView?
ViewPager Fragment OncreateView根据切换的标签数多次调用