2016-05-07 13 views
2

を消えない編集:フローティング操作ボタン "+" をクリックすると、bottomsheetはこのように表示さアンドロイド:bottomsheetは通常

package com.walkindeep.teammanagerpreview; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.design.widget.BottomSheetBehavior; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.SwipeRefreshLayout; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

import com.dexafree.materialList.card.Card; 
import com.dexafree.materialList.card.CardProvider; 
import com.dexafree.materialList.card.OnActionClickListener; 
import com.dexafree.materialList.card.action.TextViewAction; 
import com.dexafree.materialList.listeners.OnDismissCallback; 
import com.dexafree.materialList.view.MaterialListView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

public class MyTaskActivity extends NavigationActivity { 
    private Context mContext = this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     /*全局导航栏*/ 
     LayoutInflater inflater = (LayoutInflater) this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View contentView = inflater.inflate(R.layout.activity_task, null, false); 
     drawer.addView(contentView, 0); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButtionTask); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       View bottomSheet = findViewById(R.id.bottom_sheet); 
       BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 
       bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
      } 
     }); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

/*cardView*/ 
//  initCard(); 
     final User user = User.init("xxx", "xxx"); 
     updateTask(user); 

     /*下滑刷新*/ 
     final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout); 
     swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       updateTask(user, swipeRefreshLayout); 
      } 
     }); 



    } 

    /*更新任务数据*/ 
    private void updateTask(User user, SwipeRefreshLayout swipeRefreshLayout) { 
     DataHandler dataHandler = new DataHandler(); 
     dataHandler.getData("issues.json?assigned_to_id=me" + "&status_id=1", this, user); 
     swipeRefreshLayout.setRefreshing(false); 
    } 

    /*更新任务数据*/ 
    private void updateTask(User user) { 
     DataHandler dataHandler = new DataHandler(); 
     dataHandler.getData("issues.json?assigned_to_id=me" + "&status_id=1", this, user); 
    } 

    private Card[] jsonToCards(JSONObject userIssuesJSONObjectTemp) { 
     JSONArray userIssuesJSONArray = null; 
     try { 
      userIssuesJSONArray = userIssuesJSONObjectTemp.getJSONArray("issues"); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     List<Card> cards = new ArrayList<>(); 

     /*用于存储所有issue的list*/ 
     List<Issue> issueList = Issue.getIssueList(); 
     issueList.clear(); 

     for (int i = 0; i < userIssuesJSONArray.length(); i++) { 
      try { 
       /*创建issue类*/ 
       Issue issue = new Issue(userIssuesJSONArray.getJSONObject(i).getString("subject"), userIssuesJSONArray.getJSONObject(i).getString("description"), userIssuesJSONArray.getJSONObject(i).getInt("id")); 
       issueList.add(issue); 

       Card card = new Card.Builder(this) 
         .withProvider(new CardProvider()) 
         .setLayout(R.layout.material_basic_buttons_card) 

         .setTitle(issue.getSubject()) 
         .setDescription(issue.getDescription()) 
         .addAction(R.id.left_text_button, new TextViewAction(this) 
           .setText("按钮1") 
           .setTextResourceColor(R.color.black_button) 
           .setListener(new OnActionClickListener() { 
            @Override 
            public void onActionClicked(View view, Card card) { 
             Toast.makeText(mContext, "You have pressed the left button", Toast.LENGTH_SHORT).show(); 
            } 
           })) 
         .addAction(R.id.right_text_button, new TextViewAction(this) 
           .setText("按钮2") 
           .setTextResourceColor(R.color.accent_material_dark) 
           .setListener(new OnActionClickListener() { 
            @Override 
            public void onActionClicked(View view, Card card) { 
             Toast.makeText(mContext, "You have pressed the right button", Toast.LENGTH_SHORT).show(); 
            } 
           })) 
         .endConfig() 
         .build(); 
       card.setTag(issue);//设置卡片和issue关联 
       cards.add(card); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
     return cards.toArray(new Card[cards.size()]); 
    } 


    public void setCardsToUI(Card[] cards) { 
     final MaterialListView mListView = (MaterialListView) findViewById(R.id.material_listview); 
     mListView.getAdapter().clearAll(); 
     for (int i = 0; i < cards.length; i++) { 
      mListView.getAdapter().add(cards[i]); 

      /*设置卡片可以滑动删除*/ 
      cards[i].setDismissible(true); 
     } 

     /*设置滑动删除操作*/ 
     mListView.setOnDismissCallback(new OnDismissCallback() { 
      public void onDismiss(final Card card, final int position) { 
       Snackbar.make(mListView, "已标记完成", Snackbar.LENGTH_LONG) 
         .setCallback(new Snackbar.Callback() { 
          @Override 
          public void onDismissed(Snackbar snackbar, int event) { 
           if (event == DISMISS_EVENT_ACTION) {//do nothing but recover card 
            mListView.getAdapter().add(position, card); 
           } else { 
            Issue issue = (Issue) card.getTag(); 
            issue.setStatusid(3); 
            issue.pushStatusName(mContext); 

            List<Issue> issueList = Issue.getIssueList(); 


           } 
          } 
         }) 
         .setAction("撤销", new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 

          } 
         }) 

         .show(); 
      } 
     }); 
    } 

    class DataHandler extends AbstractDataQuery { 
     @Override 
     protected void work(JSONObject userIssuesJSONObject) { 
      Card[] cards = jsonToCards(userIssuesJSONObject); 
      setCardsToUI(cards); 
     } 
    } 
} 

enter image description here

ここactivity_task.xmlファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MyTaskActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_task" /> 
    </LinearLayout> 

<ScrollView 
    android:layout_width="match_parent" 
    android:id="@+id/bottom_sheet" 
    android:layout_height="wrap_content" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 



     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <Button 
       android:id="@+id/buildProjectButton" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/buildProject" /> 

      <Button 
       android:id="@+id/buildTaskButton" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/buildTask" /> 

     </LinearLayout> 

</ScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/floatingActionButtionTask" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_input_add" /> 
</android.support.design.widget.CoordinatorLayout> 

すべてがOKと思われるが、私はbottomsheetの上をクリックしたときに、それは私が期待として完全に消えていません:

enter image description here

何が問題なのですか。

+0

あなたの子ビューで '線形レイアウト'の代わりに 'フレームレイアウト'を使用してください。私の答えをチェックしてください。 –

答えて

-1

まず、この問題を解決するための動機付けをしてくれたjaydroiderに感謝します。

キーは、底部シートがではなく、がデフォルトで非表示になっています。だからあなたはする必要があります

bottomSheetBehavior.setHideable(true); 
+0

upvote brotherと私の答えを気に入ってください。ありがとう:) –

2

CoordinatorLayout(すなわち、app:layout_behavior=”android.support.design.widget.BottomSheetBehavior”)追加して、あなたは自動的に取得します適切なタッチ検出の子ビューにBottomSheetBehaviorを取り付けることにより、5つの状態の間を移行することが...」。あなたのLinear LayoutFrame Layoutに変更しようとします。

は次のように使用してみてくださいください。

<FrameLayout 
    android:id="@+id/bottom_sheet" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ff0000" 
    app:behavior_hideable="true" 
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> 

     <Button 
      android:id="@+id/buildProjectButton" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/buildProject" /> 

     <Button 
      android:id="@+id/buildTaskButton" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/buildTask" /> 


</FrameLayout> 

コードを次のように変更します。

View bottomSheet = findViewById(R.id.bottom_sheet); 
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 


bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 
    @Override 
    public void onStateChanged(@NonNull View bottomSheet, int newState) { 
     // React to state change 
     Log.e("onStateChanged", "onStateChanged:" + newState); 
     if (newState == BottomSheetBehavior.STATE_EXPANDED) { 
       // Hide your state here. 
     } 
    } 

    @Override 
    public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
     // React to dragging events 
     Log.e("onSlide", "onSlide"); 
    } 
}); 

EDIT 1:

ボトムシートの挙動は、デフォルトではhideableではありません。これを追加する必要があります。あなたはPersistentボトムシートを使用している

bottomSheetBehavior.setHideable(true); 
+0

動作しませんでした。それでも同じ問題。 – guo

+0

私のケースでは期待通りに 'Bottom Sheet'が消えています。私はあなたのコードをこのケースで見る必要があります。上記の質問にあなたの 'Activity'コードを投稿してください。 –

+0

@guo私は自分の答えを更新しました。あなたが問題に直面したら私に知らせてください。 –

0

、ボトムシートのこのタイプは、あなたがそれを下にドラッグして、それを閉じる必要がありdrag eventsすなわち、主に応答します。ダイアログの外をクリックすることに反応するボトムシートが必要な場合は、Modalボトムシートを検討してください。 また、fabを使用して、下部シートの状態を切り替えることもできます。onClickfabは、折りたたまれている場合はシートを展開するか、展開した場合は折りたたむ必要があります。

+0

ボトムシートをModalボトムシートに変更するには? – guo

関連する問題