再次调用“ onCreateView”-Android

时间:2018-06-23 06:53:59

标签: android android-fragments android-listfragment

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

有关该功能的一些解释: 该函数发送一些参数以创建新的卡视图。 在接收参数的函数中,存在一个循环,该循环使用“获取计数”创建卡堆栈。 我需要在运行时更改“获取计数” ...

感谢您的帮助!

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

1 个答案:

答案 0 :(得分:0)

只需创建方法例如。 updateUI 将添加卡或更新数据逻辑放入此方法,每当获取数据时,便将数据传递给此方法。