3

フレームレイアウトを含むカスタム ビューがあります。このフレームレイアウトには、スワイプできる 2 つのビュー (LinearLayout) が含まれています。最初のものをスワイプすると、2 番目のものが表示され、その逆も同様です。これらのビューの 1 つにボタンがありますが、理由はわかりません。このボタンは無効に似ています。クリックできず、onClick メソッドは効果がありません。

ここでは、カスタム ビューでインフレートされたレイアウト xml の構造を示します。

<FrameLayout>

  <LinearLayout
      android:id="@+id/frontview"
  /> 
  <LinearLayout
      android:id="@+id/backview">
    <Button
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:id="@+id/ButtonUpdate"
       android:text="@string/bUpdate"
       android:padding="5dp"
       android:clickable="true"
       style="?android:attr/borderlessButtonStyle"
        />
  </LinearLayout>

</FrameLayout>

ここに私のカスタムビューのコード:

public class mView extends LinearLayout {

ImageView icon;
TextView current_data;
TextView previous_data;
TextView time ;
Button bUpdate;
EditText TextUpdate;

public mView(Context context) {
    super(context);
    init(context);
}

public mView(Context context, AttributeSet attrs) {
    super(context,attrs);
    init(context);

}

public mView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    // nothing
    }
}

public void init(Context pContext) {
    LayoutInflater inflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View ll = inflater.inflate(R.layout.list_item_data, this, true);

    /** We initialize the elements of our UI **/
    /**
     * First View
     */
    icon= (ImageView) ll.findViewById(R.id.ic_icon);
    current_data = ll.(TextView) findViewById(R.id.current_data);
    previous_data = ll.(TextView) findViewById(R.id.previous_data);
    time = (TextView) ll.findViewById(R.id.time);

    /**
     * Second View
     */
    bUpdate = (Button) ll.findViewById(R.id.ButtonUpdate);
    TextUpdate= (EditText) ll.findViewById(R.id.TextUpdate);
    bUpdate.setOnClickListener(new bUpdateClickListener());        
}

private class bUpdateClickListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        // When the button is clicked, the front view re-appears and the backview disappears
        frontview
                .animate()
                .translationX(0);
        backview
                .animate()
                .translationX(-backview.getMeasuredWidth());

    }
}

スワイプは と で正しく処理されonInterceptTouchEvent(MotionEvent ev)ますonTouchEvent(MotionEvent ev)

MyActivity で使用される main.xml は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#ffffff">

   <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#f6f6f6">

      <TextView
         android:id="@+id/infoimc"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="18dp"
         android:layout_marginTop="8dp"
         android:text="@string/app_name"
         android:textColor="#000000"
         android:textSize="16dp"/>
      <View
         android:id="@+id/divider_infoimc"
         android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginRight="18dp"
            android:layout_marginLeft="18dp"
            android:background="#99CC00"/>
      <com.example.essai.CustomGraph
         android:id="@+id/CustomGraph"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:layout_gravity="center"
         android:visibility="visible"
         android:layout_marginTop="10dp"/>

   </LinearLayout>

   <FrameLayout
      android:layout_width="wrap_content"
      android:layout_height="80dp"
      android:layout_gravity="left|center_vertical">

   <com.example.essai.mView
      android:id="@+id/CustomView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

   </FrameLayout>

</LinearLayout>

そして MyActivity.class :

public class MyActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   }

}

ボタンをアクティビティでも処理する必要があるかどうかわかりませんか?

ご協力いただきありがとうございます !

4

1 に答える 1

3

根本原因

APIによると:

FrameLayout は、画面上の領域をブロックして1 つの項目を表示するように設計されています。

あなたの場合のように、より多くのアイテムがある場合、「予期しない」ことが起こります。

子ビューはスタックに描画され、最後に追加された子が一番上になります。

これは、あなたfrontviewがあなたの上にbackviewあり、クリックイベント(ボタン上)がないため、下に委任されていないことを意味しますfrontviewandroid:clickable="true"

解決策 1

子レイアウトのグラビティをプログラムで並べ替えます。

ただし、複数の子を FrameLayout に追加し、属性を使用して各子に重力を割り当てることで、FrameLayout 内の位置を制御できandroid:layout_gravityます。

スライドするたびにandroid:layout_gravity="top"とを切り替えるだけです。android:layout_gravity="bottom"

解決策 2

子レイアウトの可視性をプログラムで制御します。

を表示するbackview必要がある場合は、 の可視性を に設定frontviewView.GONEます。そしてView.VISIBLE、逆の場合に設定します。

解決策 3

FrameLayout別のレイアウト タイプに変更します。

レイアウトxmlでさらに「いじる」必要があるかもしれません...

あなたが最もよく知っているものを見て、それに応じて解決策を選択してください:)

于 2013-10-26T08:00:57.483 に答える