gpt4 book ai didi

android - 再次调用 "onCreateView"- Android

转载 作者:太空狗 更新时间:2023-10-29 14:39:25 25 4
gpt4 key购买 nike

我正在尝试调用函数“onCreateView”(在第一个代码中)并在下面的第二个代码中调用它。我需要在代码运行时向堆栈添加一个 fragment 。如果有另一种好方法,我很乐意听到。

关于功能的小解释:该函数发送一些参数来创建新的卡片 View 。在接收参数的函数中,有一个循环使用“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++;
}
}

主要 Activity :

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);
}
});
}
}

最佳答案

只需创建一个方法ex。 updateUI 将您的添加卡或更新数据逻辑放在此方法中,每当您获取数据时,然后将数据传递给此方法。

关于android - 再次调用 "onCreateView"- Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com