2017-11-11 25 views
-1

私はAndroidの開発は初めてですが、私はSwing/WPF(C#)GUIの経験がかなりあります。私がしようとしていることは次のとおりです:再利用可能なビュー?

私はBottomNavigationViewに3つの別々のビューを持っています。私の問題は、フラグメントのうちの2つのインターフェースの一部は、同一のセットで構成されていることである

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.one: 
       findViewById(R.id.one).setVisibility(View.VISIBLE); 
       findViewById(R.id.two).setVisibility(View.GONE); 
       findViewById(R.id.threee).setVisibility(View.GONE); 
       return true; 
    // ... and so on 

:各ビューはOnNavigationItemSelectedListener介しMainActivity Javaコードに隠され/示されている断片として実装されていますEditTextおよびImageButton。だから私はコードを倍増する代わりに、何かを再利用できるものにすることを考えました。私の選択肢はフラグメント内にフラグメントを作ることです(ただし、インターネット上ではこれはお勧めできません)、再利用可能なビューを作成することです(https://developer.android.com/training/improving-layouts/reusing-layouts.htmlhttps://developer.android.com/guide/topics/ui/custom-components.html)。それを行う最善の方法は何ですか?私はEditTextButtonsとのやり取りに耳を傾けることができる、接続されたJavaコードを持つxmlレイアウトを持っていきたいと思います。たぶん私は私のWPFの経験に偏っているが、私はxaml + codebehindのようにしようとしているが、これはAndroidアプリを構築するときに問題に取り組む正しい方法ではないかもしれない。

+0

Javaコード内の同じビューで行うために、同じ方法を使用する場合は、あなたのアプリケーションロジックに応じて、同じことを行うには、静的メソッドを宣言したが、私はどのように答えを与えていますビューを再利用することはできますが、それが私の質問にコメントしたいものではない場合、私は自分の答えを編集するものを説明してください! – Xenolion

答えて

0

あなたが

... 
<include layout="@layout/your_frequent_used_layout"/> 

<TextView android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:padding="10dp" /> 
.... 
のようなあなたのレイアウト内 <include>タグを使用します、あなたがXMLで

を複数回と

を使用するビューの個別のXMLレイアウトを定義する必要があります

詳細情報here in the documentation training!

Java

Java側の場合特定のビューを取得して現在のレイアウトに追加するにはInflaterというレイアウトが必要で、次にあなたのアクティビティにこのようにViewを追加してください(あなたのフラグメントでは、

第一オプション:

View view = null; 
LayoutInflater inflater = 
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.your_reusable_view, null); 
your_main_root_view.addView(view); 

第二オプション:

View child = getLayoutInflater().inflate(R.layout.your_reusable_view, null); 
your_main_root_view.addView(child); 
+0

ありがとうございました。これらのEditTextとButtonによって発生したイベントを聞くことができるクラスを持つことは可能ですか?または、MainViewにリスナーを実装する必要がありますか? – Marco

+0

ええ、インターフェイスとメソッドを持つクラスを作成し、mainViewクラスのインターフェイスを実装します。 – Xenolion

+0

私はそれを得ることができるように全力を注いでください!それに答える! – Xenolion

0

のCustomViewを使用してこれを行う方法です。これで同じレイアウトファイルを使用できるだけでなく、カスタム表示の実装でこのビューと同様のやりとりを定義することもできます。

1

質問内のコードごとに、レイアウトに表示されているビューを表示/非表示するだけです。
まず、BottomNavigationViewViewPagerと組み合わせて使用​​し、すべてのナビゲーションアイテムに対して独立したフラグメントを作成することをお勧めします。
この記事では、一般的な項目を含むすべてのフラグメントは、あなたがすべての一般的なロジックを管理します抽象BaseFragmentを作成するために、第二に http://droidmentor.com/bottomnavigationview-with-viewpager-android/


を開始する方法を示しています。
この抽象フラグメントを拡張して、個々のフラグメントを作成し、ユニークなレイアウトを膨張させます。

fragment_base.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!--- Your Common Elements Here ---> 

    <!--- Unique Elements will get inserted here ---> 

</LinearLayout> 

fragment_nav_unique1.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!--- Unique Elements For the Nav View Item ---> 

</LinearLayout> 

BaseFragment.java

public abstract class BaseFragment extends Fragment { 

    // Get Layout resource id of the Individual Fragments 
    protected abstract int layoutResourceId(); 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     // Inflate the base layout 
     LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_base, container, false); 

     // Inflate the unique layouts and add them to the view  
     LinearLayout uniqueView = (LinearLayout) inflater.inflate(layoutResourceId(), null); 
     view.addView(uniqueView); 

     // Return view 
     return view; 
    } 

    //--- Manage all the Common Elements here --- 

} 

FirstFragment.java

public class FirstFragment extends BaseFragment { 
    @Override 
    protected int layoutResourceId() { 
     // Return the resource of the unique layout 
     return R.layout.fragment_nav_unique1; 
    } 

    //--- Manage all the Unique Elements here --- 

} 
関連する問題