If I have a Fragment with 3 possible layouts, how can I swap the layouts at runtime?

huangapple go评论76阅读模式
英文:

If I have a Fragment with 3 possible layouts, how can I swap the layouts at runtime?

问题

所以我将会有一个包含3个布局文件的片段。
所有3个布局文件都包含一个文本视图和2个按钮。

第一个布局文件只有文本视图和2个按钮。

第二个布局文件只有文本视图和3个按钮。

第三个布局文件只有文本视图和4个按钮。

当片段被创建时,我将知道需要哪个布局文件,并可以使用switch语句在它们之间进行选择。

我的困惑来自于如何定义和构建对象/界面元素。

由于我可以在onCreateView中返回每个布局文件的视图,这没问题。

但是如何只构建必要数量的按钮呢?

因为根据情况,我只会有2个按钮可见,但一个类要控制所有4个。

英文:

So I am going to have a Fragment with 3 layout files.
All 3 Layout files have one textview and 2 buttons.

The first layout file has only the textview and 2 buttons.

The second layout file has only the textview and 3 buttons

The third layout file has only the textview and 4 buttons.

When the fragment is created, I will know which layout file is required and could use a switch statement to choose between them.

My confusion is coming from how to define construct the objects/ui elements.

Since I could return the view in the oncreateView with each layout file no problem.

But how would I only construct the necessary number of buttons?

Since depending on the situation I would only have 2 buttons visible but one class controlling all 4.

答案1

得分: 2

你不需要为此使用三种不同的布局。
只需根据需要更改额外按钮的可见性。

将默认布局与TextView和两个按钮进行填充,然后添加您的逻辑。

thirdButton.visibility = if (shouldDisplayThirdButton) View.VISIBLE else View.GONE
fourthButton.visibility = if (shouldDisplayFourthButton) View.VISIBLE else View.GONE
英文:

You don't need 3 different layouts for this.
Just change the extra buttons visibility depending on your need.

Inflate default layout with textview and 2 buttons and then add your logic.

thirdButton.visibility = if(shouldDisplayThirdButton) View.Visible else View.Gone
fourthButton.visibility = if(shouldDisplayFourthButton) View.Visible else View.Gone

答案2

得分: 0

你可以拥有一个布局,根据某些条件仅切换按钮的可见性:

myButton.visibility = View.VISIBLE或 View.GONE

或者你可以创建片段布局,如下所示:

<FrameLayout>
   <LayoutWith2Buttons>
   <LayoutWith3Buttons>
   <LayoutWith4Buttons>
</FrameLayout>

然后在片段中切换这些布局的可见性:

myLayoutWith2Buttons.visibility = View.VISIBLE或 View.GONE
英文:

you can have one layout and just switch buttons visibility based on some conditions:

myButton.visibility = View.Visible(or View.Gone)

or you can create fragment layout like this:

FrameLayout
   LayoutWith2Buttons
   LayoutWith3Buttons
   LayoutWith4Buttons

and in fragment switch visibility of those layouts

myLayoutWith2Buttons.visibility = View.Visible (or View.Gone)

答案3

得分: 0

以下是您提供的代码的翻译部分:

我不知道您想要获得什么但您也可以使用带有 PageAdapter 的 ViewPager

class CustomVPAdapter extends PagerAdapter {
    private String[] pagesList;
    private Context context;

    public CustomVPAdapter(Context context, String[] pagesList) {
        this.pagesList = pagesList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return this.pagesList.length;
    }

    @Override
    public boolean isViewFromObject(android.view.View view, Object object) {
        return (view == (ConstraintLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        LayoutInflater layoutInflater = ((LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        android.view.View view = layoutInflater.inflate(R.layout.custome_pager_adapter, container, false);
        final ImageView localImageView = (ImageView) view.findViewById(R.id.iv_page);

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((ConstraintLayout) object);
    }
}

请注意,这是您提供代码的中文翻译部分。如果您有任何其他需要,请随时提问。

英文:

I donot know what you want to get but you can also use ViewPager with PageAdapter

class CustomVPAdapter extends PagerAdapter {
private String[] pagesList;
private Context context;


public CustomVPAdapter(Context context, String[] pagesList) {
    this.pagesList= pagesList;
    this.context = context;
   
}

@Override
public int getCount() {
    return this.pagesList.length;
}

@Override
public boolean isViewFromObject(android.view.View view, Object object) {
    return (view == (ConstraintLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    LayoutInflater layoutInflater = ((LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
    android.view.View view = layoutInflater.inflate(R.layout.custome_pager_adapter, container, false);
    final ImageView localImageView = (ImageView) view.findViewById(R.id.iv_page);

   
  
   container.addView(view);
    return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((ConstraintLayout) object);
}

}

huangapple
  • 本文由 发表于 2020年9月15日 19:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63900631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定